python爬虫入门三:requests库
urllib库在很多时候都比较繁琐,比如处理Cookies。因此,我们选择学习另一个更为简单易用的HTTP库:Requests。
1. 什么是Requests
Requests是用python编写,基于urllib,采用Apache2 Licensed开源协议的HTTP库。它比urllib更加简单易用。
2. 使用Requests库
2.1 构建请求
使用requests可以很方便的构建请求:
r = requests.get('https://api.github.com/events')# 构建GET请求r = requests.post('http://httpbin.org/post') # 构建POST请求r = requests.put('http://httpbin.org/put') #构建PUT请求
2.1.1 GET请求
import requests
response = requests.get('http://httpbin.org/get')
print(response.text)
2.1.2 带参数的GET请求
使用GET进行请求数据时,其参数全部位于url中。
要使用requests构建一个带参数的GET请求,可以选择将参数和url拼接形成新的url进行请求,或者是将需要传递的参数传入requests.get()方法的params
# 方法一:将拼接后的url传入requests.get()
response = requests.get('http://httpbin.org/get?name=gemey&age=22')
# 方法二:将参数传入requests.get()的params
data = {
'name': 'gemey',
'age': 22
}
response = requests.get('http://httpbin.org/get', params=data)
2.1.3 POST请求
POST请求和GET请求最显著的区别在于,其请求的参数需要与url分离开来。
在requests中,构建POST请求的方法和构建带参数的GET请求方法类似,只需要将需要传递的参数参数requests.post()方法的data
import requests
data ='}
response = requests.post('http://httpbin.org/post', data=data)
print(response.text)
2.1.4 解析json
在python中,要解析json数据,经常使用json.loads()方法。同时,requests也提供了requests.json()方法用于解析数据。
这二者效果一致
import requests
import json
response = requests.get('http://httpbin.org/get')
print(type(response.text))
print(response.json())
# 二者效果一致
print(json.loads(response.text))
2.1.5 获取二进制数据
# 以github一个图标为例
import requests
response = requests.get('https://github.com/favicon.ico')
# requests.content 即为获取的二进制数据
print(type(response.content))
print(response.content)
2.1.6 添加headers信息
为get()的headers参数传入相应的headers信息即可
import requests
# 需要传入的headers信息
header = {
'User-Agent': 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50'
}
response = requests.get('http://zhihu.com/explore', headers=header)
print(response.text)
2.2 Response
2.2.1 response属性
print(response.status_code) # 返回状态码 print(response.headers) # 返回headers信息 print(response.cookies) # 返回cookies信息 print(response.url) # 返回访问的url print(response.history) # 访问历史记录
2.2.2 response状态码判别
内置状态码判别:requests.codes包含了一系列访问正常的状态码,可用于判别是否连接成功:
import requests
response = requests.get('http://www.jianshu.com')
# 使用内置状态码判定
exit() if not response.status_code==requests.codes.ok else print('Request Successfully')
# 直接判定
exit() if not response.status_code==200 else print('Request Successfully')
2.3 高级操作
2.3.1 文件上传
文件上传,使用POST请求。
import requests
files = {'file': open('favicon.ico', 'rb')}
response = requests.post('http://httpbin.org/post', files=files)
print(response.text)
2.3.2 获取cookie
response.cookies获取到的是键值对,可以把其中每一条记录都提取出来。
import requests
response = requests.get('https://baidu.com')
print(respnse.cookies)
for key, value in response.cookies.items():
print(key, value)
2.3.3 会话维持
cookies的作用就是给网站用于维持会话,常用于模拟登陆。
使用requests中的session()对象,使得前后两次访问都使用统一浏览器,即维持会话,cookies保持一致。
s = requests.Session()
s.get('http://httpbin.org/cookies/set/number/123456789')
response = s.get('http://httpbin.org/cookies')
print(response.text)
2.3.4 证书验证
在打开使用https协议的页面时,浏览器首先需要对该网页的证书进行验证。如果验证不通过,程序就会报错。比如曾经的12306网站。
因此,就需要设置verify=False关闭证书验证。
response = requests.get('https://www.12306.cn', verify=False)
print(response.status_code)
同时,我们也可以选择使用本地的证书用于验证网页。只需要在cert里传入本地证书即可。
response = requests.get('https://www.12306.cn', cert=('/path/server.crt'))
print(response.status_code)
2.3.5 代理设置
requests使用代理设置,只需要在请求时传入proxies参数
proxies = {
'http': 'http://123.0.0.1:23',
'https': 'https://123.0.0.1:123'
}
response = requests.get('https://www.taobao.com', proxies=proxies)
print(response.status_code)
2.3.6 超时设置
为访问网页设置超时,如果访问网页超过规定的时间,就抛出异常。目的是防止在请求某个网页时时间过长导致程序没有继续运行。
在请求时设置timeout参数即可设置请求网页时耗费的最大时长。如果超过这个时间还没有请求完,程序即报错。
使用requests的exceptions模块进行处理异常报错。
import requests
from requests.exceptions import ReadTimeout
try:
response = requests.get('http://httpbin.org/get', timeout=0.5)
print(response.status_code)
except ReadTimeout:
print('Timeout')
2.3.7 认证设置
在访问某些网站时,需要进行登陆验证。可以在请求时设置auth,传入认证的账户密码
import requests
from requests.exceptions import ReadTimeout
try:
response = requests.get('http://httpbin.org/get', timeout=0.5)
print(response.status_code)
except ReadTimeout:
print('Timeout')
2.3.8 异常处理
使用requests的exceptions模块
import requests
from requests.exceptions import ReadTimeout, HTTPError, RequestException
try:
response = requests.get('http://httpbin.org/get', timeout=0.5)
print(response.status_code)
except ReadTimeout:
print('timeout')
except HTTPError:
print('http error')
except RequestException:
print('error')
2.4 保存抓取到的图片
如果我们从网页中爬取图片,那么拿到每一张图片对应的url后,我们需要把图片保存到本地。
urllib库中有一个urlretrieve()方法可以将图片保存到本地,但是requests库中并没有这个功能,我们需要使用文件写入的方法将图片保存到本地
# 以百度首页的logo为例# import requests
# 找到logo对应的url
url = 'https://www.baidu.com/img/bd_logo1.png'
r = requests.get(url)
# 写入本地磁盘# 'wb'是指以二进制格式打开一个文件只用于写入。如果该文件已存在则将其覆盖。如果该文件不存在,创建新文件。
with open('/path/baidu.png', 'wb') as f: f.write(r.content)
with...as...的方法可以在写入文件完成后自动关闭文件,不需要我们手动将文件关闭,因而可以避免因忘记关闭文件而浪费资源和干扰已经写入的文件。
关于使用open()写入文件的具体方式可以参考:python打开与关闭文档
或者,使用urllib的urlretrieve()方法
import urllib url = 'https://www.baidu.com/img/bd_logo1.png' urllib.request.urlretrieve(url, '/path/baidu.png')
python爬虫入门三:requests库的更多相关文章
- PYTHON 爬虫笔记三:Requests库的基本使用
知识点一:Requests的详解及其基本使用方法 什么是requests库 Requests库是用Python编写的,基于urllib,采用Apache2 Licensed开源协议的HTTP库,相比u ...
- 3.Python爬虫入门三之Urllib和Urllib2库的基本使用
1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解释才呈现出来的,实质它是一段HTML代码,加 JS.CSS ...
- 转 Python爬虫入门三之Urllib库的基本使用
静觅 » Python爬虫入门三之Urllib库的基本使用 1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器 ...
- Python爬虫入门之Urllib库的基本使用
那么接下来,小伙伴们就一起和我真正迈向我们的爬虫之路吧. 1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解 ...
- Python爬虫入门之Urllib库的高级用法
1.设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Headers 的属性. 首先,打开我们的浏览 ...
- Python爬虫入门——使用requests爬取python岗位招聘数据
爬虫目的 使用requests库和BeautifulSoup4库来爬取拉勾网Python相关岗位数据 爬虫工具 使用Requests库发送http请求,然后用BeautifulSoup库解析HTML文 ...
- Python 爬虫入门(requests)
相信最开始接触Python爬虫学习的同学最初大多使用的是urllib,urllib2.在那之后接触到了第三方库requests,requests完全能满足各种http功能,真的是好用爆了 :D 他们是 ...
- Python爬虫的开始——requests库建立请求
接下来我将会用一段时间来更新python爬虫 网络爬虫大体可以分为三个步骤. 首先建立请求,爬取所需元素: 其次解析爬取信息,剔除无效数据: 最后将爬取信息进行保存: 今天就先来讲讲第一步,请求库re ...
- 芝麻HTTP: Python爬虫利器之Requests库的用法
前言 之前我们用了 urllib 库,这个作为入门的工具还是不错的,对了解一些爬虫的基本理念,掌握爬虫爬取的流程有所帮助.入门之后,我们就需要学习一些更加高级的内容和工具来方便我们的爬取.那么这一节来 ...
随机推荐
- 【模板】平衡树——Treap和Splay
二叉搜索树($BST$):一棵带权二叉树,满足左子树的权值均小于根节点的权值,右子树的权值均大于根节点的权值.且左右子树也分别是二叉搜索树.(如下) $BST$的作用:维护一个有序数列,支持插入$x$ ...
- 2个rman自动恢复的脚本
### scripts 1--the scirpt is used for restore db from vcs to a point to time recovery--and the targe ...
- 遍历list集合的三种方式
List<String> list1 = new ArrayList<String>(); list1.add("1"); list1.add(" ...
- ecshop文章分类页面调用文章的内容
有的时候需要用到,所以总结了一下. 打开includes/lib_article.php文件 红色部分为添加的部分 function get_cat_articles($cat_id, $page = ...
- 如何查找Oracle某列值相同的字段
相关的sql语句如下 select xm_guidfrom T_NZYDKgroup by xm_guidhaving count (*)>1
- CodeSmith Generator 7.0.2
[工具]CodeSmith Generator 7.0.2激活步骤 只看楼主 收藏 回复 M炎骫毒逆天T c#攻城狮 8 学过三层的人应该认识CodeSmith Generator吧, ...
- DSO的接口文档[转]
本文从别处转来: (开发环境)使用前先注册一下DSOFramer.ocx 操作:将DSOFramer.ocx复制到C:\windows\system32目录下, 开始->运行->regsv ...
- 原来MFC窗口样式随字符集而改变
以前好像发现,MFC窗口上按钮的自动样式有时是有亮色边框3D效果的,有时没有,不知道原因,也没有追究,今天正好有机会发现了原因,原来是随字符集而改变的. 1.Unicode版本下的窗口 2.未设置的窗 ...
- openssl 安装配置
Openssl是个为网络通信提供安全及数据完整性的一种安全协议,囊括了主要的密码算法.常用的密钥和证书封装管理功能以及SSL协议,并提供了丰富的应用程序供测试或其它目的使用.首先下载Openssl包: ...
- 学习cocos2dx3.1.0
static_cast<type-id>expression 该运算符把expression转换为type-id类型 Lambda表达式 CallFunc::create([=](){} ...