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!,从请求的响应结果可以看到数据已上传到服务端中。