login with urllib2

Chris Rebert clp2 at rebertia.com
Sun Aug 14 11:07:37 EDT 2011


2011/8/14 守株待兔 <1248283536 at qq.com>:
> please to see my code:
> import urllib
> import urllib2
>
> url = 'http://hi.baidu.com/'
> values = {'username' : '******','password' : '******' }
>
> data = urllib.urlencode(values)
> req = urllib2.Request(url,data)
> response = urllib2.urlopen(req)
> the_page = response.read()
>
> i can't to login ,why?username and password  are  right.
> any word  appreciated.

Use the URL specified as the <form>'s action, which in this case is
https://passport.baidu.com/?login , rather than the URL of the login
page itself.
The <form> fields aren't called "username" and "password"; read the
page's HTML. You'll also see that there are a few hidden form fields
that are probably required.
Finally, even if you fix these problems, the site could possibly block
Python based on its User-agent header.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list