I have heard that requests's library is strong, but has not contacted, today contacted a bit, found before using URLLIB,URLLIB2 and other methods are really too rubbing ...
Here to write some simple use preliminary as a record
This article continues to practice the use of Requests login website, Renren has a feature is the sign-in function, need to log in every day to upgrade.
The following code Python code implements the process of using requests to log on to a Web site.
The following is a complete HTTP request header using the Fiddler capture package:
Python3 use requests to sign in to Renren website. PY Code:
"" "Python3 use requests to login to Renren website. py2016 May 11 07:33:59 codegay references requests Documentation: http://cn.python-requests.org/zh_CN/ latest/Four common POST-submission data modes https://imququ.com/post/four-ways-to-post-data-in-http.html "" "Import reimport requests# Requests Install Command: Pip install requestsloginurl= ' http://www.zimuzu.tv/User/Login/ajaxLogin ' surl= ' http://www.zimuzu.tv/ User/sign ' httphead={' Accept ': ' Application/json, Text/javascript, */*; q=0.01 ', ' Origin ': ' http://www.zimuzu.tv ', ' X-requested-with ': ' XMLHttpRequest ', ' user-agent ': ' mozilla/5.0 (Windows NT 6.3; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/50.0.2661.94 safari/537.36 ', ' content-type ': ' application/ X-www-form-urlencoded ',}data= "account= user name &password= password &remember=1" session=requests. Session () login=session.post (loginurl,data=data,headers=httphead) print (login.cookies) #打印登录后取得到cookies对象print ( Login.json ()) Getstat=session.get (sURL). Text.split ("\ n") #访问签到页面, showing the last three login times [Print (R) for R in Getstat if "Three login Time" in R]
Compared with the two can be seen, there are some HTTP headers omitted to achieve the purpose, after all, after all the manual request of the head feel very troublesome.
In Fidder connection:keep-alive content-length: Two items can not be omitted, NCAT and other tools can not omit content-length, if you change the post data, The value of content-length needs to be corrected manually.
Content-length can be omitted in Python, I guess Python has handled it for us.
About Python3 use requests to login to everyone's film and television website to introduce so much to everyone, hope to help you!