我正在尝试使用机械化登录网站。 weibsite没有表格。有人可以帮帮我吗?问题是什么? 谢谢
Python代码:
import mechanize
browser = mechanize.Browser()
browser.open("http://website.com/login.php")
print "forms-start"
for form in browser.forms():
print form
print "forms-end"
Outut:
forms-start
forms-end
网站:
<body>
<div align="center">
<div class="msg"></div><br/>
<form method="POST" action="" autocomplete="off">
<p><span><b>Username*:</b></span> <input type="text" name="username" placeholder="your account name" value="" style="width:18em" /></p>
<p><span><b>Password*:</b></span> <input type="password" name="password" placeholder="your account password" value="" style="width:18.0em" /></p>
<p><span><b>Remember*:</b></span> <input type="checkbox" name="remember" value="1" checked/></p>
<input type="hidden" name="login" value="1" />
<input type="submit" class="frmbtn" value="Login" />
</form>
</div>
</body>
答案 0 :(得分:1)
根据FAQ Mechanize无法处理无效的HTML
喜欢:&#34; br /&#34; 您在website 上有这样的代码您可以使用BeautifullSoup Parser
import mechanize
browser = mechanize.Browser(factory=mechanize.RobustFactory())
browser.open("http://example.com/")
print browser.forms
或者,您可以任意处理HTML(和标题):
browser = mechanize.Browser()
browser.open("http://example.com/")
html = browser.response().get_data().replace("<br/>", "<br />")
response = mechanize.make_response(
html, [("Content-Type", "text/html")],
"http://example.com/", 200, "OK")
browser.set_response(response)
来自FAQ