Library use requests
>>> properties
Keep-Alive & Connection Pool
Internationalized Domain Name and URL
With persistent cookie session
Browser-style SSL certification
Automatic content decoding
Basic / Digest authentication
The elegant key / value cookie
Automatic decompression
Unicode response body
HTTP (S) Agent
Block file uploads
Stream download
Connection timed out
Block Request
Support .netrc
2 request method
response = requests.get(‘https://httpbin.org/get’) response = requests.post(‘http://gttpbin.org/post’,data={‘key’:'value’})
3 passing URL parameters
params = {‘key1’:‘value1’,‘key2’:‘value2’}
response = requests.get(‘http://httpbin.org/get’,params=params)
4 Custom Headers
headers = {‘user-agent’:‘my-app/0.0.1’} #自定义headers response = requests.get(url,headers=headers)
5 Custom cookies
co = {‘cookies_are’:‘working’}
response = requests.get(url,cookies=co)
6 set the proxy
proxies = { ‘http’:‘http://10.10.1.10:3128’, ‘https’:‘https://10.10.1.10:1080’ } requests.get(‘http://httpbin.org/ip’,proxies=proxy)
7 redirect
response = requests.get(‘http://github.com’,allow_redirects=False)
8 prohibit certificate validation
= requests.get Response ( 'http://httpbin.org/post',verify= False) # but closed after verification, there is a more annoying warning, the following methods may be used to close the warning From requests.packages.urllib3.exceptions Import InsecureRequestWarning requests.packages.urllib3.disable_warnings (InsecureRequestWarning)
9 Set timeout
requests.get(‘http://github.com’,timeout=0.01)
Receiving a response
>>> Character Encoding
response = requests.get(‘https://api.github.com/events’) response.encoding = ‘utf-8’ print(response.text)
>>> binary data
response = requests.get(‘https://api.github.com/events’) print(response.content)
>>> json data
response = requests.get(‘https://api.github.com/events’) print(response.json())
>>> status code
response = requests.get(‘http://httpbin.org/get’) print(response.status_code)
>>> server returns cookies
response = requests.get(url) print(response.cookies[‘example_cookie_name’])
>>> session objects
session = requests.Session() session.get(‘http://httpbin.org/cookies/set/sessioncookie/123456789’) response = session.get(‘http://httpbin.org/cookies’) print(response.text) #{“cookies”: {“sessioncookie”: “123456789”}}