第六天:requests库的用法
每天上午9点左右更新一到两篇文章到专栏《Python爬虫训练营》中,对于爬虫有兴趣的伙伴可以订阅专栏一起学习,完全免费。
键盘为桨,代码作帆。这趟为期30天左右的Python爬虫特训即将启航,每日解锁新海域:从Requests库的浪花到Scrapy框架的深流,从反爬迷雾中的破局到数据清洗的澄澈。我们拆解网页结构如同解读星图,让XPath与正则表达式化作导航罗盘。每个深夜的代码调试,终将凝结成破晓时的数据宝藏。训练营不设码头,结营之日,正是你独自远征星辰大海的起点。
文章目录
一. 介绍
二. 安装
三. 基本请求
1.get请求
2.post请求
3.自定义请求头部
4.设置超时时间
5.代理访问
7.ssl验证
四、获取响应信息
一. 介绍
对了解一些爬虫的基本理念,掌握爬虫爬取的流程有所帮助。入门之后,就需要学习一些更加高级的内容和工具来方便我们的爬取。这一节来简单介绍一下 requests 库的基本用法。
二. 安装
利用 pip 安装
pip install requests
三. 基本请求
req = requests.get("http://www.baidu.com")
req = requests.post("http://www.baidu.com")
req = requests.put("http://www.baidu.com")
req = requests.delete("http://www.baidu.com")
req = requests.head("http://www.baidu.com")
req = requests.options("http://www.baidu.com")
1.get请求
参数是字典,我们也可以传递json类型的参数:
import requests
url = "http://www.baidu.com/s"
params = {'wd': '爱学习'}
response = requests.get(url, params=params)
print(response.url)
response.encoding = 'utf-8'
html = response.text
print(html)
2.post请求
参数是字典,我们也可以传递json类型的参数:
url = "http://www.sxt.cn/index/login/login.html"
formdata = {
"user": "17703181473",
"password": "123456"
}
response = requests.post(url, data=formdata)
response.encoding = 'utf-8'
html = response.text
print(html)
3.自定义请求头部
伪装请求头部是采集时经常用的,我们可以用这个方法来隐藏:
import requests
headers = {'User-Agent': 'python'}
r = requests.get('http://www.zhihu.com', headers = headers)
print(r.request.headers['User-Agent'])
4.设置超时时间
可以通过timeout属性设置超时时间,一旦超过这个时间还没获得响应内容,就会提示错误
requests.get('http://github.com', timeout=0.001)
5.代理访问
采集时为避免被封IP,经常会使用代理。requests也有相应的proxies属性
import requests
proxies = {
"http": "http://10.10.1.10:3128",
"https": "https://10.10.1.10:1080",
}
requests.get("http://www.zhidaow.com", proxies=proxies)
如果代理需要账户和密码,则需这样:
proxies = {
"http": "http://user:pass@10.10.1.10:3128/",
}
6.session自动保存cookies
seesion的意思是保持一个会话,比如 登陆后继续操作(记录身份信息) 而requests是单次请求的请求,身份信息不会被记录。
# 创建一个session对象
s = requests.Session()
# 用session对象发出get请求,设置cookies
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
7.ssl验证
# 禁用安全请求警告
requests.packages.urllib3.disable_warnings()
resp = requests.get(url, verify=False, headers=headers)
四、获取响应信息
代码 | 含义 |
resp.json() | 获取响应内容(以json字符串) |
resp.text | 获取响应内容 (以字符串) |
resp.content | 获取响应内容(以字节的方式) |
resp.headers | 获取响应头内容 |
resp.url | 获取访问地址 |
resp.encoding | 获取网页编码 |
resp.request.headers | 请求头内容 |
resp.cookie | 获取cookie |