Python爬虫系列(4)requests库的基本使用
1.为什么学习requests
学习了上一节,大家应该都有体会,urlib编码解码很麻烦,同时cookie信息的保存,还需要借助cookjar,但是本节学习的requests库都帮我们把这些问题解决了。
2.安装requests库
pip install requests
3.requests库的使用
3.1发送GET请求
1.最简单的get请求就是通过requests.get来调用的
import requests
resp = requests.get("http://www.baidu.com/")
#resp.text会根据requests默认的解码方式进行解码,可能会造成乱码
print(resp.text)
#resp.content返回的数据类型是bytes型,在网络上和硬盘传输都是这种类型
print(resp.content)
#自己指定解码方式进行解码
print(resp.content.decode('utf-8'))
#查看完整的url地址
print(response.url)
#查看头部响应头部的编码
print(response.encoding)
#查看响应码
print(response.status_code)
2.添加headers和查询参数
# get请求(2)
import requests
url = 'https://www.baidu.com/s'
kw = {'wd': 'python'}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36'
,
'Cookie': 'BIDUPSID=040B6CF0BDB654FFB00A0F216C516E40; PSTM=1567587333; BD_UPN=12314753; BDUSS=TQ5WnlIWTI5VlNVUnVBZS1IU2hQU05VRGZXS1N4b3lkc2huYjNYUlZ-RVlyNlZkRVFBQUFBJCQAAAAAAAAAAAEAAACxAmx8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABgifl0YIn5dZ; MCITY=-148%3A; BAIDUID=C2FB9E266AFBEA6512C2AACC28C87740:FG=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BD_HOME=1; H_PS_PSSID=1446_21120_26350; delPer=0; BD_CK_SAM=1; PSINO=6; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; COOKIE_SESSION=198_1_2_7_5_2_0_0_2_2_0_0_271905_0_0_0_1580985413_1580713469_1581041230%7C9%231533405_26_1580713465%7C9; H_PS_645EC=458a1rBPXdGaBobgypLzxWA5Kdl3EBpikY%2FgxXgQtcK3p2jTG%2B6Z2wDHlm4'
}
resp = requests.get(url, params=kw, headers=headers)
with open('python.html', 'w', encoding='utf-8') as f:
f.write(resp.content.decode('utf-8'))
print(resp.url)
3.2发送POST请求
1.最基本的POST请求可以使用post方法
resp = request.post("http://www.baidu.com/",data=data)
2.传入data数据
这时候不再使用urlencode进行编码了,直接传入一个字典进去就可以了,以拉勾网为列子
import requests
url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'
index_url = "https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput="
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36',
'Referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=',
}
resp_0 = requests.get(index_url, headers=headers)
cookie = resp_0.cookies
data = {
'first': 'true',
'pn': '1',
'kd': 'python'
}
resp = requests.post(url, data=data, headers=headers, cookies=cookie)
print(resp.json())
3.使用代理
使用requests添加代理也非常简单,只要在请求方法中(比如get 或者 post)传递proxies参数就可以了,示例代码如下
import requests
url = "http://www.httpbin.org/ip"
proxy = {
'http': '221.229.252.98:9797'
}
resp = requests.get(url, proxies=proxy)
print(resp.text)
4.cookie:
如果在一个响应中包含了cookie,那么可以利用cookies属性拿到这个返回的cookie值。
import requests
url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'
index_url = "https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput="
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36',
'Referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=',
}
resp = requests.get(index_url, headers=headers)
#获取响应中的cookie
cookie = resp.cookies
5.session
之前使用urllib库,是可以使用opener发送多个请求,多个请求之间可以共享cookie.如果使用requests,也要达到共享cookie的母第,那么可以使用requests库给我们提供的session对象。注意,这里的session不是web开发中的哪个session,这个地方只是一个绘画的对象而已,下面以拉勾网举一个例子。
import requests
url = 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput='
url2 = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36'
,'Referer': 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput'
}
session = requests.session()
session.get(url, headers=headers)
data = {
'first': 'true',
'pn': '1',
'kd': 'python'
}
resp = session.post(url2, headers=headers, data=data)
print(resp.text)
6.处理不信任的SSL证书:
对于那些已经呗信任的SSL证书的网站,比如百度,那么使用requests直接就可以正常的返回响应,示例代码如下:
resp = requests.get("http://www.12306.cn/mormhweb/",verify=False)
print(resp.content.decode('utf-8'))