How to download a webpage that require username an

2020-03-06 07:11发布

问题:

For example, I want to download this page after inserting username and password:

http://forum.ubuntu-it.org/

I have tryed with wget but doesn't work.

Is there a solution with python ?

You can test with these username and password:

username: johnconnor
password: hellohello

回答1:

Like @robert says, use mechanize.

To get you started:

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!"


回答2:

Try the mechanize module. It's basically a programmatic browser interface.



回答3:

You can use the urllib2 module and with that it is possible do to basic and form based authentication (with cookies support).

Here is a nice tutorial on your issue.