使用 mechanize 在 wordpress 上发帖
长话短说,我需要向我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
博客中内置的代码可能正在检测用户代理字符串并拒绝显示评论,但我对 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?