requests模塊(請求接口)
下面分別是get,post,入參json,添加cookie,添加header,上傳/下載文件 的接口請求舉例:
import requests #導入模塊
#1.發get請求
url = 'http://api.xx**.cn/api/xx/stu_info'
data = {'stu_name':'xx'} #請求數據
req = requests.get(url,params=data) #發get請求
print(req.json()) #返回字典類型
print(req.text()) #返回string,json串
#2.發post請求
url = 'http://api.xx**.cn/api/xx/login'
data = {'username':'lyl','passwd':'23456'} #請求數據
req = requests.post(url,data) #發post請求
print(req.json()) #返回json串
#3.入參是json類型的
import random
# phone=random.randint(10000000000,99999999999)
url='http://api.xx**.cn/api/xx/add_stu'
data = {
"name":"小1",
"grade":"天蠍座",
"phone":phone,
"sex":"男",
"age":28,
"addr":"河南省濟源市北海大道32號"
}
req = requests.post(url,json=data) #請求數據
print(req.json())
#4、添加cookie
url = 'http://api.xx**.cn/api/xxx/gold_add'
data = {'stu_id':468,'gold':10000}
djl = {'fancy':'337ca4cc825302b3a8791ac7f9dc4bc6'}
req = requests.post(url,data,cookies=djl)
print(req.json())
#5.添加header
url = 'http://api.xx**.cn/api/xxx/all_stu'
header = {
'Referer':'http://api.xx**.cn/'
}
req = requests.get(url,headers=header)
print(req.json())
#6、上傳文件
url= 'http://api.xx**.cn/api/file/file_upload'
data = {
'file':open(r'C:\Users\bxxddeng\Desktop\圖\6bd9026dt935575932465&690.jpg','rb')
}
req= requests.post(url,files=data)
print(req.json())
#7、下載文件
url = 'http://up.mcyt.net/?down/46779.mp3'
req = requests.get(url)
fw = open('aaa.mp3','wb')
fw.write(req.content)
python爬蟲使用requests發送post請求示例詳解
簡介
HTTP協議規定post提交的數據必須放在消息主體中,但是協議並沒有規定必須使用什么編碼方式。服務端通過是根據請求頭中的Content-Type字段來獲知請求中的消息主體是用何種方式進行編碼,再對消息主體進行解析。具體的編碼方式包括:
application/x-www-form-urlencoded 最常見post提交數據的方式,以form表單形式提交數據。
application/json 以json串提交數據。
multipart/form-data 一般使用來上傳文件。
一、 以form表單發送post請求
Reqeusts支持以form表單形式發送post請求,只需要將請求的參數構造成一個字典,然后傳給requests.post()的data參數即可。
例:
1
2
3
4
5
6
7
8
|
# -*- coding: utf-8 -*-
# author:Gary
import
requests
data
=
{
'key1'
:
'value1'
,
'key2'
:
'value2'
}
# 你發送給這個的數據
r
=
requests.post(url, data
=
data)
# 使用requests的post方法,data接受你想發送的數據
print
(r.text)
# 查看返回的內容
|
輸出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
{
“args”: {},
“data”: “”,
“files”: {},
#你提交的表單數據
“form”: {
“key1”: “value1”,
“key2”: “value2”
},
“headers”: {
……
“Content
-
Type
”: “application
/
x
-
www
-
form
-
urlencoded”,
……
},
“json”: null,
……
}
|
可以看到,請求頭中的Content-Type字段已設置為application/x-www-form-urlencoded,且data = {‘key1': ‘value1', ‘key2': ‘value2'}以form表單的形式提交到服務端,服務端返回的form字段即是提交的數據。
二、 以json形式發送post請求
可以將一json串傳給requests.post()的data參數,
1
2
3
4
5
6
7
8
9
|
# -*- coding: utf-8 -*-
# author:Gary
import
requests
import
json
json_data
=
json.dumps({
'key1'
:
'value1'
,
'key2'
:
'value2'
})
# 你發送給這個的數據,數據格式轉為json
r
=
requests.post(url, data
=
json_data)
# 使用requests的post方法,data接受你想發送的數據
print
(r.text)
# 查看返回的內容
|
輸出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
{
“args”: {},
“data”: “{\”key2\”: \”value2\”, \”key1\”: \”value1\”}”,
“files”: {},
“form”: {},
“headers”: {
……
“Content
-
Type
”: “application
/
json”,
……
},
“json”: {
“key1”: “value1”,
“key2”: “value2”
},
……
}
|
可以看到,請求頭的Content-Type設置為application/json,並將json_data這個json串提交到服務端中。
三、 以multipart形式發送post請求(上傳文件)
Requests也支持以multipart形式發送post請求,只需將一文件傳給requests.post()的files參數即可。
1
2
3
4
5
6
7
8
|
# -*- coding: utf-8 -*-
# author:Gary
import
requests
files
=
{
'file'
:
open
(
'report.txt'
,
'rb'
)}
# 目錄下得有report.txt文件才能上傳,rb是指以二進制格式打開一個文件用於只讀。
r
=
requests.post(url, files
=
files)
# 通過files參數指定你想發送的文件的內容
print
(r.text)
|
輸出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
{
“args”: {},
“data”: “”,
“files”: {
“
file
”: “Hello world!”
},
“form”: {},
“headers”: {……
“Content
-
Type
”: “multipart
/
form
-
data; boundary
=
467e443f4c3d403c8559e2ebd009bf4a
”,
……
},
“json”: null,
……
}
|
文本文件report.txt的內容只有一行:Hello world!,從請求的響應結果可以看到數據已上傳到服務端中。