如何下载需要用户名和密码的网页?
比如,我想在输入用户名和密码后下载这个页面:
http://forum.ubuntu-it.org/
我试过用wget,但没成功。
有没有用python的解决办法?
你可以用这些用户名和密码来测试:
username: johnconnor
password: hellohello
3 个回答
1
试试这个叫做 mechanize 的模块。它基本上是一个可以通过代码来操作的浏览器界面。
1
就像@robert说的,使用mechanize这个工具。
下面是一些入门的内容:
from mechanize import Browser
b = Browser()
b.open("http://forum.ubuntu-it.org/index.php")
b.select_form(nr=0)
b["user"] = "johnconnor"
b["passwrd"] = "hellohello"
b.submit()
response = b.response().read()
if "Salve <b>johnconnor</b>" in response:
print "Logged in!"