获取网络数据
python中使用第三方库requests来获取网络数据
import requests
- 确定请求的地址(url)
url = 'https://www.apiopen.top/satinApi'
GET传参的两种方式:
a.直接拼接在url后面(?参数=值&参数2=值2....)
b.给get函数中的第二个参数传参({参数:值, 参数2:值2...})
- 通过GET方式请求数据,请求结束后会返回响应
response = requests.get(url,{'type': 1, 'page': 1}) - 获取数据内容
a.获取字符串形式的数据
print(type(response.text), response.text)
b.获取json格式的数据
reslut = response.json()
print(type(reslut), reslut)
c.获取二进制形式的数据
data = response.content
print(type(data), data)
图片下载
1.创建url
url='http://cdnq.duitang.com/uploads/item/201505/13/20150513212949_j5aJU.thumb.700_0.jpeg'
2.获取网络数据
response = requests.get(url)
3.拿二进制数据
data = response.content
print(data)
4.保存图片
with open('./files/luffy.jpeg', 'bw') as f:
f.write(data)