网络爬虫之requests库中两个重要的对象
来源:互联网 发布:淘宝买白酒是真的吗 编辑:程序博客网 时间:2024/05/18 12:44
当我们使用resquests.get()时,返回的时response的对象,他包含服务器返回的所有信息,也包含请求的request的信息。
首先:
response对象的属性有以下几个,
r.status_code是http请求的返回状态,200表示连接成功,404表示连接失败,这时候应该抛出异常,进行处理。
r.text是url对应的页面内容
r.encoding是从http的header中猜测的响应内容编码方式
r.apparent_encoding是从内容中分析出响应的内容编码方式。
r.content是http响应内容的二进制形式
通用的代码框架
try:
r=requests.get(url,timeout=30)
r.raise_for_status()#如果不是200,就会抛出异常
r.encoding=r.apparent_encoding
return r.text
except:
return “产生异常”
0 0
- 网络爬虫之requests库中两个重要的对象
- 从零开始学网络爬虫之Requests库
- Python网络爬虫之Requests库
- requests库---网络爬虫
- 初始网络爬虫之三:网络爬虫最常用的requests库
- 1、网络爬虫之规则之requests库入门
- Python 网络爬虫与信息获取(一)—— requests 库的网络爬虫
- python网络爬虫1.1requests库
- requests库,爬虫的第一步
- 网络爬虫:Requests+lxml
- Python爬虫利器一之Requests库的用法
- Python爬虫利器一之Requests库的用法
- Python爬虫利器一之Requests库的用法
- 爬虫系列之Requests库入门
- Pythoon爬虫之requests库学习
- Python爬虫之requests
- 爬虫之 requests小结
- python爬虫之requests
- 文件中读取数字并排序输出到另一个文件中
- PS流
- js变量存在的两种类型(原始值,引用值)
- C++ 快速排序算法
- 递归删除多级文件夹及文件
- 网络爬虫之requests库中两个重要的对象
- 排序之简单选择排序
- vertical-align属性详解
- 函数调用
- hdoj 5444 Elven Postman (二叉树 创建+查找)
- hadoop-note 1
- ElasticSearch(ES)中的分片查询方式
- 野指针
- Spark集群启动之Master、Worker启动流程源码分析