Python simulates login to Sina mail using the mechanism, pythonmechanism
Preparation of related knowledge about mechanics:
Machize. Browser () <br> # Set whether to process the HTML http-equiv Header
Set_handle_equiv (True) <br> # Set whether to process redirection
Set_handle_redirect (True) <br> # Set whether to add a referer header to each request
Set_handle_referer (True) <br> # The setting does not comply with the rules in robots.
Set_handle_robots (False) <br> # handle giz Transfer Encoding
Set_handle_gzip (False) <br> # Set browser header information
The logon code is as follows:
Import machizebr = machize. browser () br. set_handle_robots (False) # indicates that the url in robots is not followed by 'HTTP: // mail.sina.com.cn/'br.addheaders = [('user-agent', '*')] br. open (url) for form in br. forms (): # view the form print formbr on the logon page. select_form (nr = 0) br ['local'] = 'username' br ['pwd'] = 'Password' response = br. submit () print 'success' for link in br. links (): print link. url + ':' + link. text