python-2.7 - 在 Python 2.7.5 上使用 Mechanize 登录 Bing

标签 python-2.7 automation mechanize bing

我正在尝试使用 Python 2.7.5 和 mechanize library创建一个程序,让我登录到我的 Microsoft 帐户 bing.com .首先,我创建了这个程序来打印这个网页上的表单名称,以便我可以在以后的代码中引用它们。我当前的代码是这样的(对长 URL 感到抱歉):

import mechanize
br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent','Firefox')]

br.open("https://login.live.com/ppsecure/post.srf?wa=wsignin1.0&rpsnv=11&ct=1375231095&rver=6.0.5286.0&wp=MBI&wreply=http:<%2F%2Fwww.bing.com%2FPassport.aspx%3Frequrl%3Dhttp%253a%252f%252fwww.bing.com%252f&lc=1033&id=264960&bk=1375231423")
print(br.title)

forms_printed = 0
for form in br.forms():
    print form
    forms_printed += 1
if forms_printed == 0:
    print "No forms to print!"

尽管当我在 Firefox 中访问网页时,我看到了用户名和密码表单,但当我运行此代码时,结果始终是“没有表单可打印!”我在这里使用 Mechanize 是错误的,还是网站故意阻止我找到这些表格?非常感谢任何提示和/或建议。

最佳答案

如果您尝试阅读您收到的 HTML,您将看到该网页需要 javascript。

例子:

import mechanize
br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent','Firefox')]

page = br.open("https://login.live.com/ppsecure/post.srf?wa=wsignin1.0&rpsnv=11&ct=1375231095&rver=6.0.5286.0&wp=MBI&wreply=http:<%2F%2Fwww.bing.com%2FPassport.aspx%3Frequrl%3Dhttp%253a%252f%252fwww.bing.com%252f&lc=1033&id=264960&bk=1375231423")
print page.read()
print(br.title)

forms_printed = 0
for form in br.forms():
    print form
    forms_printed += 1
if forms_printed == 0:
    print "No forms to print!"

输出:
Microsoft account

JavaScript required to sign in
Microsoft account requires JavaScript to sign in. This web browser either does not support JavaScript, or scripts are being blocked.

To find out whether your browser supports JavaScript, or to allow scripts, see the browser's online help.

related questions关于那个

关于python-2.7 - 在 Python 2.7.5 上使用 Mechanize 登录 Bing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18028911/

相关文章:

python - 在类 "major flaw"中实现装饰器?

python - Pygame - Sprite 与 Sprite 组的碰撞

compiler-construction - 语义分析语言?

Python Mechanize 动态下拉选择

python - 如何在 python 中将 .txt 转换为 .xls?

ant - 从 CSV 文件自动创建图表的工具(在 ANT 中)

在 r 中重复运行一个函数,直到不产生错误。

python - 如何登录我的网上银行账户并打印交易记录?

Python: Mechanize 找不到任何形式

python - 在保持标签的同时将大范围压缩为 python 中的较短范围