1. Installation
1 git clone git://github.com/kennethreitz/requests.git2CD requests3 Python setup.py Install
2. Power on the DOT
(GET)
1>>>ImportRequests2>>> URL ='http://dict.baidu.com/s'3>>> payload = {'WD':'python'}4>>> r = Requests.get (url,params=payload)//The most basic GET request with parameters5<response [200]>6>>>Print(R.url)7Http://dict.baidu.com/s?wd=python8>>>Print(R.text)//decoded data
(POST)
1>>>ImportRequests2>>>ImportJSON3>>> r = Requests.post ('Https://api.github.com/some/endpoint', Data=json.dumps ({'some':'Data'}))4>>>Print(R.json ())5{u'Documentation_url': U'Https://developer.github.com/v3', u'message': U'Not Found'}
Custom Header
1>>>ImportRequests2>>>ImportJSON3>>> data = {'some':'Data'}4>>> headers = {'Content-type':'Application/json','user-agent':'mozilla/5.0 (x84_64;) firefox/22.0'}5>>> r = Requests.post ('Https://api.github.com/some/endpoint', data=data,headers=headers)6>>>Print(R.text)7 {8 "message":"Not Found",9 "Documentation_url":"Https://developer.github.com/v3"Ten}
Timeout settings
1 requests.get ( " http://github.com , Timeout=0.001) 2 3 ----------------------4 a bunch of exceptions, focus on the back 5 connecttimeout: Httpconnectionpool (Host= " github.com " Span style= "color: #800000;" > ' , port=80): Max retries exceeded with URL:/(Caused by connecttimeouterror (< Requests.packages.urllib3.connection.HTTPConnection object at 0x01eae6f0>, connection to github.com timed out. (Connect timeout=0.001) )
accessing cookies
1 Import Requests 2 r = requests.get ('http://www.google.com.hk/')3Print (r.cookies['NID'])4print(tuple ( R.cookies))
You can also send cookies to the server
1 ImportRequests2URL ='http://myserver.org/cookies'3cookies = {'Testcookies_1':'Hello_python3','testcookies_2':'hello_requests'}4 #in Cookie Version 0, special symbols such as spaces, brackets, parentheses, equals, commas, double quotes, slashes, question marks, @, colons, semicolons, etc., are not allowed as the contents of the cookie. 5r = Requests.get (URL, cookies=cookies)6 Print(R.json ())
Python third-party library requests Learning