网络爬虫--requests库中两个重要的对象
当我们使用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 “产生异常”
网络爬虫--requests库中两个重要的对象的更多相关文章
- python爬虫#网络请求requests库
中文文档 http://docs.python-requests.org/zh_CN/latest/user/quickstart.html requests库 虽然Python的标准库中 urlli ...
- Python爬虫—requests库get和post方法使用
目录 Python爬虫-requests库get和post方法使用 1. 安装requests库 2.requests.get()方法使用 3.requests.post()方法使用-构造formda ...
- 4.爬虫 requests库讲解 GET请求 POST请求 响应
requests库相比于urllib库更好用!!! 0.各种请求方式 import requests requests.post('http://httpbin.org/post') requests ...
- 爬虫requests库的基本用法
需要注意的几个点: 1.后面的s是一个虚拟目录 2.url后面不用加问号,发起请求的时候会自动帮你加上问号 get_url = 'http://www.baidu.com/s' 3. url的特性:u ...
- [爬虫] requests库
requests库的7个常用方法 requests.request() 构造一个请求,支撑以下各种方法的基础方法 requests.get() 获取HTML网页的主要方法,对应于HTTP的GET re ...
- 5.爬虫 requests库讲解 高级用法
0.文件上传 import requests files = {'file': open('favicon.ico', 'rb')} response = requests.post("ht ...
- Python网络爬虫——BeautifulSoup4库的使用
使用requests库获取html页面并将其转换成字符串之后,需要进一步解析html页面格式,提取有用信息. BeautifulSoup4库,也被成为bs4库(后皆采用简写)用于解析和处理html和x ...
- 【Python成长之路】Python爬虫 --requests库爬取网站乱码(\xe4\xb8\xb0\xe5\xa)的解决方法【华为云分享】
[写在前面] 在用requests库对自己的CSDN个人博客(https://blog.csdn.net/yuzipeng)进行爬取时,发现乱码报错(\xe4\xb8\xb0\xe5\xaf\x8c\ ...
- Python爬虫 requests库基础
requests库简介 requests是使用Apache2 licensed 许可证的HTTP库. 用python编写. 比urllib2模块更简洁. Request支持HTTP连接保持和连接池,支 ...
随机推荐
- USB gadget学习笔记
1.usb-OTG-ADP-HNP-SRP https://blog.csdn.net/xiongjiao0610/article/details/44150849
- C# winform javascript 互调用
1.准备环境 vs2010 2. 项目结构 index.htm 为需要显示的web页面 其中需要操作提供的方式的C# 调用的javascript 方法 以及进行测试javascript 调用C# 方法 ...
- C语言多线程pthread库相关函数说明
线程相关操作说明 一 pthread_t pthread_t在头文件/usr/include/bits/pthreadtypes.h中定义: typedef unsigned long int pth ...
- const_cast
函数原型: const_cast < type-id > ( expression ) 去掉const属性:const_cast<int*> (&num),常用,因为不 ...
- Android自动化测试-UiAutomator2环境搭建
Android自动化测试-UiAutomator环境搭建(QQ交流群:490451176) 一.环境准备 1. 安装android sdk,并配置环境变量 2. 安装android studio,国内 ...
- 64位windows 2003和windows xp
msdn windows2003 64位简体中文企业版R2 sp2(cn_win_srv_2003_r2_enterprise_x64_with_sp2_vl) ed2k://|file|cn_win ...
- Run-time Settings 运行时设置
1.执行顺序设置和执行概率设置 2.迭代之间的等待时间设置 3.日志设置 4.思考时间 5.杂项 线程进程选择: 一般服务器没有安全机制选择线程执行 但是有安全机制的话 比如第一个进程50个线程 第二 ...
- Esxi5-管理平台vcenter5.0_数据库迁移流程
migrating-vcenter-database-express-to-sql-2008-r2 一. 准备环境. ESXi5.0主机 IP:192.168.1.158 ...
- phpstorm破解 IntelliJ IDEA License Server本地搭建教程 http://blog.lanyus.com/archives/174.html/comment-page-6#comments 附件:mac环境
ilanyu's Blog 搜索关键字搜索 首页IDEA注册码文件中转在线记事本老博客关于 IntelliJ IDEA License Server本地搭建教程 作者: ilanyu 时间: Marc ...
- python requests 爬取数据
import requests from lxml import etree import time import pymysql import json headers={ 'User-Agent' ...