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

python uses mechanize to simulate logging into Sina mailbox

高洛峰
Release: 2017-02-13 17:36:47
Original
1870 people have browsed it

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=&#39;http://mail.sina.com.cn/&#39;
br.addheaders = [(&#39;User-agent&#39;, &#39;*&#39;)]
br.open(url)
for form in br.forms():#查看登录页面中的表单
    print form
br.select_form(nr=0)
br[&#39;local&#39;]=&#39;用户名&#39;
br[&#39;pwd&#39;]=&#39;密码&#39;
response=br.submit()
print &#39;success&#39;
for link in br.links():
    print link.url+&#39;:&#39;+link.text
Copy after login

For more articles related to python using mechanize to simulate logging into Sina mailbox, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template