Python Analog HTTP request 2

Source: Internet
Author: User

Found a very useful third-party module:requests, the analog interface is very simple.

For more information, please visit: http://docs.python-requests.org/en/latest/

Very good

#!coding:utf-8 believe that we all know, do not explain

#导入需要的requests

Import requests

#定义请求头

reqheaders={' content-type ': ' application/x-www-form-urlencoded ', ' Accept ': ' Text/html,application/xhtml+xml, application/xml;q=0.9,*/*;q=0.8 ', ' Host ': ' www.renren.com ', ' Origin ': ' http://zhichang.renren.com ', ' Referer ': ' http ://zhichang.renren.com ', ' user-agent ': ' mozilla/5.0 (Windows NT 6.1) applewebkit/537.1 (khtml, like Gecko) chrome/ 21.0.1180.89 safari/537.1 ',}

#定义post的参数, requests module, parameter not coded

reqdata={' email ': ' [email protected] ', ' Password ': ' xxxx ', ' autologin ': ' On ', ' Origurl ': ' http://zhichang.renren.com/? Login_state=rr ', ' domain ': ' renren.com '}

#模拟post请求, do not allow automatic redirection

Res=requests.post ("Http://www.renren.com/PLogin.do", Data=reqdata,headers=reqheaders,allow_redirects=false)

#模拟post请求, this is automatic redirection.

#res =requests.post ("Http://www.renren.com/PLogin.do", Data=reqdata,headers=reqheaders)

#打印服务器返回的状态

Print (Res.status_code)

#打印服务器返回的内容

Print (res.content)

#打印重定向的URL地址

Print (res.headers[' location ')

#打印服务器返回的cookie

Print (res.headers[' Set-cookie ')

The following is the result of running the program

302

The URL has moved <a href= "HTTP://ZHICHANG.RENREN.COM/?LOGIN_STATE=RR" >here</a>

Anonymid=hrt38gcmbjulj4; domain=.renren.com; path=/; Expires=sun, 17-feb-2019 11:27:44 GMT, _de=97fb170a42b4342d1c47a157ad77aafc1383380866d39ff5; domain=.renren.com; path=/; Expires=fri, 13-feb-2015 11:27:44 GMT, p=31991a0a194c34e606ef1263317b06372; domain=renren.com; path=/; Expires=thu, 20-mar-2014 11:27:44 GMT, ap=229996362; domain=renren.com; path=/; Expires=thu, 20-mar-2014 11:27:44 GMT, first_login_flag=1; domain=renren.com; path=/, t=7f5cc046700354f79346b8b51f2361882; domain=.renren.com; path=/, T=B28114AD2246BAD1B6B67F07B3B6C0A3; domain=renren.com; path=/xtalk/, societyguester=7f5cc046700354f79346b8b51f2361882; domain=.renren.com; path=/, id=229996362; domain=.renren.com; path=/, xnsid=bb3f9425; domain=.renren.com; path=/, Loginfrom=syshome; domain=.renren.com; path=/

Python Analog HTTP request 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.