使用 mechanize 在 wordpress 上发帖

发布于 2024-12-17 02:22:13 字数 786 浏览 1 评论 0原文

长话短说,我需要向我的 WordPress 博客发布自动评论。我找到了 Python 的 mechanize(万岁!无需在 PHP 中乱搞!),但我无法用它发表评论。根据调试信息我登录成功并返回评论页面,但评论不显示。更奇怪的是,当我尝试两次发布相同的内容时,我收到“检测到重复评论”的反馈,就好像它已经发布过一样……WTF?我的代码的相关部分:

br = mechanize.Browser()

cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

br.set_debug_redirects(True)
br.set_debug_responses(True)
br.set_debug_http(True)

logger = logging.getLogger('mechanize')
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.INFO)

br.open('some wordpress comment page')

response = br.follow_link(url_regex=r'wp-login')

br.select_form(nr=0)
br['log'] = username
br['pwd'] = passwd
br.submit()

# the above logs me in successfully

br.select_form(nr=1)
br['comment'] = comment

response = br.submit()

...不起作用。有什么想法吗?

Long story short I need to post automated comments to my WordPress blog. I find mechanize for Python (hurray! no need to muck around in PHP!) but I can't post comments with it. According to the debug info I logged in successfully and returned to the comment page, but comments don't show. What's weirder is that when I try posting the same thing twice, I get the feedback "duplicate comment detected" as if it had already been posted... WTF? Revelant bits of my code:

br = mechanize.Browser()

cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

br.set_debug_redirects(True)
br.set_debug_responses(True)
br.set_debug_http(True)

logger = logging.getLogger('mechanize')
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.INFO)

br.open('some wordpress comment page')

response = br.follow_link(url_regex=r'wp-login')

br.select_form(nr=0)
br['log'] = username
br['pwd'] = passwd
br.submit()

# the above logs me in successfully

br.select_form(nr=1)
br['comment'] = comment

response = br.submit()

...doesn't work. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风柔一江水 2024-12-24 02:22:13

博客中内置的代码可能正在检测用户代理字符串并拒绝显示评论,但我对 Wordpress 代码或此 python 模块缺乏经验。

否则,mechanize 看起来就像垃圾邮件发送者最好的朋友。哦,当然有成千上万的合法用途(当然我不是在指责你)。

您能否查看您的博客日志(如果您使用的是 Apache,则为 access_log)并查看您的脚本使用什么内容访问您的博客?

it's possible that code built into the blog is detecting the user agent string and refusing to show the comments, but I have little experience with either Wordpress code or this python module.

mechanize looks like a spammer's best friend otherwise. Oh of course there are thousands of legitimate uses (and I'm not accusing you of course).

Could you look into the logs of your blog (access_log if you are using Apache) and see what your script is hitting your blog with?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文