概述
Requests 是开源的 HTTP 库,即:模拟浏览器向web服务器发请求
- 支持使用cookie保持会话,可实现登录操作
- 支持文件上传
- 更少的代码实现更多的功能
官方文档:
中文:
http://docs.python-requests.org/zh_CN/latest/index.html
https://requests.readthedocs.io/zh_CN/latest/user/quickstart.html
安装
在命令提示符下执行:
pip install requests -i https://pypi.douban.com/simple
可能需要更新pip:
pip3 install -i https://pypi.douban.com/simple --upgrade pip
使用
官方中文文档很详细,这里根据真实案例讲解
抓取58同城
模拟浏览器向web服务器发请求
import requests
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
}
url="https://cc.58.com/ershouche/?PGTID=0d100000-0013-fd90-fff0-cc46418023e7&ClickID=4"
# 发送get请求,指定 headers
resp=requests.get(url,headers=headers)
#响应内容
print(resp.text)
更多的返回信息
import requests
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
}
url="https://cc.58.com/ershouche/?PGTID=0d100000-0013-fd90-fff0-cc46418023e7&ClickID=4"
#发送get请求
with requests.get(url,headers=headers) as resp:
#打印状态码,英文提示
print(resp.status_code,resp.reason)
#返回的编码
print(resp.encoding)
#请求的url
print(resp.url)
#响应头,特殊的字典类型,key不区分大小写
print(resp.headers)
#响应内容
print(resp.text)
with open(r"c:/Users/mym/Desktop/python/requests_std/1.html","w",encoding='utf-8') as f:
f.write(resp.text)
返回gzip数据可以自动识别
import requests
#发送get请求
with requests.get('http://www.sohu.com') as resp:
#打印状态码,英文提示
print(resp.status_code,resp.reason)
#返回的编码
print(resp.encoding)
#请求的url
print(resp.url)
#响应头,特殊的字典类型,key不区分大小写
print(resp.headers)
#响应内容
print(resp.text)
服务器返回json,自动转换成dict类型
url='https://www.toutiao.com/api/pc/feed/?min_behot_time=0&category=__all__&utm_source=toutiao&widen=1&tadrequire=true&as=A1159C54E1BF01F&cp=5C413FB0A16F8E1&_signature=cEebYhAdLAnaaNviXVnx8nBHm3'
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
}
with requests.get(url,headers=headers) as resp:
j=resp.json()
print(type(j))
print(j)
for key,value in resp.cookies.items():
print(key,'==',value)
下载图片
import requests
# 这是一个图片的url
url = 'http://yun.itheima.com/Upload/Images/20170614/594106ee6ace5.jpg'
with requests.get(url) as resp:
# 获取的文本实际上是图片的二进制数据
img = resp.content
# 将他拷贝到本地文件 w 写 b 二进制 wb代表写入二进制
path='./'+url[url.rfind('/')+1:]
print(path)
with open( path,'wb' ) as f:
f.write(img)
会话维持
import requests
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
}
session = requests.session()
# 指定 headers
session.headers = headers
#session.get('http://httpbin.org/cookies/set/number/12456')
response = session.get('http://httpbin.org/cookies')
print(response.text)