Module Installation:pip Install requests
Usage Explanation:
1. Basic GET Request:
>>> r = Requests.get ("Http://httpbin.org/get") >>> print (R.text)
2. Get request with Parameters:
data = {' name ': ' Test ', ' page ': '}>>> r = Requests.get ("Http://httpbin.org/get", Params=data) >>> Print (r.text) "url": "Http://httpbin.org/get?name=test&page=10"
3. Parsing JSON:
r = Requests.get ("Http://httpbin.org/get") >>> Dict1 = R.json () >>> dict1{' args ': {}, ' headers ': {' Accept ': ' */* ', ' accept-encoding ': ' gzip, deflate ', ' Connection ': ' Close ', ' Host ': ' httpbin.org ', ' user-agent ': ' python-requests/2.18.4 '}, ' origin ': ' 125.82.191.49 ', ' url ': ' Http://httpbin.org/get '}
4. Get Binary data:
r = Requests.get ("Http://blog.51cto.com/favicon.ico") >>> with open (' Favicon.ico ', ' WB ') as F:f.write ( R.content)
5. Add headers:
Header = {' user-agent ': ' mozlila/5.0 '}>>> r = Requests.get ("Http://httpbin.org/get", Headers=header) >> > Print (r.text)
6. Basic POST Request:
data = {' name ': ' Text ', ' age ': '}>>> ' r = Requests.post ("Http://httpbin.org/post", Data=data) >>> Print (R.text)
Response: Reponse Property
Reponse.status_code: Status Code
Reponse.headers:http Head Information
Reponse.cookies:
Reponse.url:
Reponse.history: Historical History of Access
Status Code judgment:
r = Requests.get ("http://www.baidu.com") >>> if not r.status_code = = 200:exit () else:print (R.text)
Advanced Operations:
1, File Upload:
Files = {' file ': Open (' 1.jpg ', ' RB ')}r = Requests.post (' http://www.baidu.com ', files=files)
2. Session Maintenance:
s = requests. Session () s.get ("http://www.baidu.com"
3. Certificate verification:
r = requests.get (' url ', cart= ('/path/server.crt ', '/path/key '))
4. Proxy settings:
Proxies = {' http ': ' http://127.0.0.1:19324 ', ' https ': ' https//127.0.0.1:19542 '}>>> r = requests.get ('/HTTP/ Www.baidu.com ', proxies=proxies) with authenticated proxy: proxies = {' http ': ' http://user:[email protected]:19452 '}r = Requests.get (' Http://www.baidu.com ', proxies=proxies) Socks Agent: Installation module: PIP3 Install requests[socks]proxies = {' http ': ' socks5:// 127.0.0.1:1222 ', ' https ': ' socks5://127.0.0.1:3222 '}>>> r = requests.get (' http://www.baidu.com ', proxies= Proxies
5. Timeout settings:
r = Requests.get (' = 1)
6, authentication settings:
r = requests.get (' url ', auth= (' user ', ' 123 ')) or from Requests.auth import httpbasicauth>>> r = Requests.get (' URL ', Auth=httpbasicauth (' user ', ' 123 '))
Python Module Learning----Requests Module