I. BACKGROUND notes
Because Python3 's urllib and Python2 have a lot of differences, and some of the urllib.request request is not quite in line with people's thinking habits (documents are quite ugly)
So I really don't want to use Python until I look at the Urllib.request document and notice the words below
Compared to requests is really good, the document is also clear; you need to install it yourself, but it is still more recommended.
1.1 Applicable version
For python2.6, python2.7, python3.4 and above, see the official note.
I'm using the latest python3.7.
1.2 Installing the Requests module
PIP Install pipenvpipenv Install requests
Second, use requests module to complete various operations 2.1 reference requests module
Import Requests
2.2 GET Request
Import requestsurl='https://www.baidu.com'= requests.get (URL) Print (R.status_code)
2.3 Post Request
Import requestsurl='https://www.baidu.com'data_post='just Put your data and use original format'= requests.post (url,data=data_post)Print (R.status_code)
2.4 Using proxies
HTTP proxy no problem, HTTPS seems a bit of a problem need to test again
ImportRequestsurl='http://docs.python-requests.org/en/master/'proxies={ 'http':'127.0.0.1:8080', 'HTTPS':'127.0.0.1:8080'}r= Requests.get (url,proxies=proxies)Print(R.status_code)
2.5 Customizing headers
ImportRequestsurl='http://docs.python-requests.org/en/master/'Headers={ 'user-agent':'self-defind-user-agent', ' COokie':'name=self-define-cookies-in Header'}r= Requests.get (url,headers=headers)Print(R.status_code)
2.6 Custom Cookies
The experiment found that cookies that are set here do not take effect if cookies are defined in the custom header
Import requests
Url= ' http://docs.python-requests.org/en/master/'
cookies={' name1 ': ' Cookie1 ', ' name2 ': ' Cookies2 '}
#cookies =dict (name1= ' cookie1 ', name2= ' cookies2 ')
R = Requests.get (url,cookies=cookies)
Print (R.status_code)
Reference:
Official Document--http://docs.python-requests.org/en/master/
Python uses the requests module to complete get/post/proxy/custom header/custom Cookies