Home  >  Article  >  Backend Development  >  python uses mechanize to simulate logging into Sina mailbox

python uses mechanize to simulate logging into Sina mailbox

高洛峰
高洛峰Original
2017-02-13 17:36:471851browse

mechanize related knowledge preparation:

mechanize.Browser()0c6dc11e160d3b678d68754cc175188a# Set whether to process the HTML http-equiv header

set_handle_equiv(True)0c6dc11e160d3b678d68754cc175188a# Set whether to handle redirection

set_handle_redirect(True)0c6dc11e160d3b678d68754cc175188a# Set whether to add a referer header to each request

set_handle_referer(True)0c6dc11e160d3b678d68754cc175188a# The setting does not comply with the rules in robots

set_handle_robots(False)0c6dc11e160d3b678d68754cc175188a# Processing giz Transfer encoding

set_handle_gzip(False)0c6dc11e160d3b678d68754cc175188a# 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn