前言
發送post的請求參考例子很簡單,實際遇到的情況卻是很復雜的,首先第一個post請求肯定是登錄了,但登錄是最難處理的。登錄問題解決了,后面都簡單了。
一、查看官方文檔
1.學習一個新的模塊,其實不用去百度什么的,直接用help函數就能查看相關注釋和案例內容。
>>import requests
>>help(requests)

2.查看python發送get和post請求的案例
>>> import requests
>>> r = requests.get('https://www.python.org')
>>> r.status_code
200
>>> 'Python is a programming language' in r.content
True
... or POST:
>>> payload = dict(key1='value1', key2='value2')
>>> r = requests.post('http://httpbin.org/post', data=payload)
>>> print(r.text)
{
...
"form": {
"key2": "value2",
"key1": "value1"
},
...
}
二,發送post請求
♦1.用上面給的案例,做個簡單修改,發個post請求
♦2.payload參數是字典類型,傳到如下圖的form里
實例代碼:
import requests#導入request模塊 import json url = 'https://httpbin.org/post'
payload= {"pw":'1011634093@qq.com',"un":"password"}#值以字典的形式傳入 response = requests.post(url=url,data=payload) print(response.text)
運行結果:
F:\Python\python.exe F:/Python/Interface_automation/post_requests.py {"args":{},"data":"","files":{},
"form":{"pw":"1011634093@qq.com","un":"password"},#
"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate",
"Connection":"close","Content-Length":"34",
"Content-Type":"application/x-www-form-urlencoded",
"Host":"httpbin.org","User-Agent":"python-requests/2.18.4"},
"json":null,"origin":"61.175.197.202","url":"https://httpbin.org/post"} Process finished with exit code 0
三、json
♦1.post的body是json類型,也可以用json參數傳入。
♦2.先導入json模塊,用dumps方法轉化成json格式。
♦3.返回結果,傳到data里
實例代碼如下:
import requests#導入request模塊 import json#導入json模塊 url = 'https://httpbin.org/post'body= {"pw":'1011634093@qq.com',"un":"password"} data_json = json.dumps(body)#轉化成json類型 response = requests.post(url=url,data=data_json,) print(response.status_code) print(response.text)
運行結果:
F:\Python\python.exe F:/Python/Interface_automation/post_requests.py 200 b'{"args":{},
"data":"{\\"pw\\": \\"1011634093@qq.com\\",
\\"un\\": \\"password\\"}","files":{},"form":{},
"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate",
"Connection":"close","Content-Length":"45","Host":"httpbin.org",
"User-Agent":"python-requests/2.18.4"},"json":{"pw":"1011634093@qq.com","un":"password"},#json類型
"origin":"61.175.197.202","url":"https://httpbin.org/post"}\n' Process finished with exit code 0
四、headers
♦4.1.我們在請求數據時也可以加上自定義的headers(通過headers關鍵字參數傳遞)有時候有的特殊的請求必須加上headers頭信息:
實例代碼:

import requests#導入request模塊 import json url = 'https://httpbin.org/post' headers = {"Connection":'keep-alive',"Host":"httpbin.org"}#值以字典的形式傳入 response = requests.post(url=url,headers=headers)#用關鍵字headers傳入 print(response.text)
數據結果:
F:\Python\python.exe F:/Python/Interface_automation/post_requests.py {"args":{},"data":"","files":{},"form":{},
"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Content-Length":"0","Host":"httpbin.org","User-Agent":"python-requests/2.18.4"},
"json":null,"origin":"61.175.197.202","url":"https://httpbin.org/post"} Process finished with exit code 0
♦4.2.headers的取出方法:
我們有一下一種取出方法:
print(response.headers)#打印出響應頭 print(response.headers['Connection'])#取得部分響應頭以做判斷: print(response.headers.get('Connection'))#或者這樣也可以
代碼實例:
import requests#導入request模塊 import json url = 'https://httpbin.org/post' headers = {"Connection":'keep-alive',"Host":"httpbin.org"}#值以字典的形式傳入 response = requests.post(url=url,headers=headers)#用關鍵字headers傳入 print(response.headers)#打印出響應頭 print(response.headers['Connection'])#取得部分響應頭以做判斷: print(response.headers.get('Connection'))#或者這樣也可以 print(response.text)
輸出結果:
F:\Python\python.exe F:/Python/Interface_automation/post_requests.py {'Connection': 'keep-alive', 'Server': 'gunicorn/19.8.1', 'Date': 'Tue, 29 May 2018 08:30:55 GMT', 'Content-Type': 'application/json', 'Content-Length': '276', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'Via': '1.1 vegur'} keep-alive#print(response.headers['Connection'])#取得部分響應頭以做判斷:
keep-alive#print(response.headers.get('Connection'))#或者這樣也可以
{"args":{},"data":"","files":{},"form":{},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Content-Length":"0","Host":"httpbin.org","User-Agent":"python-requests/2.18.4"},"json":null,"origin":"61.175.197.202","url":"https://httpbin.org/post"} Process finished with exit code 0
♦在我看來不管是get請求還是post請求我們都是發送我們想要發送的數據給服務器,然后查看服務器相應回來的數據看看這些書是否和我們想要的內容是否相符,只是get和post的請求機制不一樣,但是所要做的思路是一樣的。
后面我會在整理一下requests模塊中的其他的東西。
