前言python
發送post的請求參考例子很簡單,實際遇到的狀況倒是很複雜的,首先第一個post請求確定是登陸了,但登陸是最難處理的。登陸問題解決了,後面都簡單了。json
1、查看官方文檔服務器
1.學習一個新的模塊,其實不用去百度什麼的,直接用help函數就能查看相關注釋和案例內容。app
>>import requests函數
>>help(requests)post
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"
},
...
}url
二,發送post請求spa
♦1.用上面給的案例,作個簡單修改,發個post請求3d
♦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
3、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
4、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模塊中的其餘的東西。