mechanize related knowledge preparation:
mechanize.Browser()<br># Set whether to process the HTML http-equiv header
set_handle_equiv(True)<br># Set whether to handle 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># Processing giz Transfer encoding
set_handle_gzip(False)<br># Set browser header information
The login code is as follows:
import mechanize br=mechanize.Browser() br.set_handle_robots(False)#表示不遵循robots中的规则 url='http://mail.sina.com.cn/' br.addheaders = [('User-agent', '*')] br.open(url) for form in br.forms():#查看登录页面中的表单 print form br.select_form(nr=0) br['local']='用户名' br['pwd']='密码' response=br.submit() print 'success' for link in br.links(): print link.url+':'+link.text
For more articles related to python using mechanize to simulate logging into Sina mailbox, please pay attention to the PHP Chinese website!