当我们使用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库中两个重要的对象的更多相关文章

  1. python爬虫#网络请求requests库

    中文文档 http://docs.python-requests.org/zh_CN/latest/user/quickstart.html requests库 虽然Python的标准库中 urlli ...

  2. Python爬虫—requests库get和post方法使用

    目录 Python爬虫-requests库get和post方法使用 1. 安装requests库 2.requests.get()方法使用 3.requests.post()方法使用-构造formda ...

  3. 4.爬虫 requests库讲解 GET请求 POST请求 响应

    requests库相比于urllib库更好用!!! 0.各种请求方式 import requests requests.post('http://httpbin.org/post') requests ...

  4. 爬虫requests库的基本用法

    需要注意的几个点: 1.后面的s是一个虚拟目录 2.url后面不用加问号,发起请求的时候会自动帮你加上问号 get_url = 'http://www.baidu.com/s' 3. url的特性:u ...

  5. [爬虫] requests库

    requests库的7个常用方法 requests.request() 构造一个请求,支撑以下各种方法的基础方法 requests.get() 获取HTML网页的主要方法,对应于HTTP的GET re ...

  6. 5.爬虫 requests库讲解 高级用法

    0.文件上传 import requests files = {'file': open('favicon.ico', 'rb')} response = requests.post("ht ...

  7. Python网络爬虫——BeautifulSoup4库的使用

    使用requests库获取html页面并将其转换成字符串之后,需要进一步解析html页面格式,提取有用信息. BeautifulSoup4库,也被成为bs4库(后皆采用简写)用于解析和处理html和x ...

  8. 【Python成长之路】Python爬虫 --requests库爬取网站乱码(\xe4\xb8\xb0\xe5\xa)的解决方法【华为云分享】

    [写在前面] 在用requests库对自己的CSDN个人博客(https://blog.csdn.net/yuzipeng)进行爬取时,发现乱码报错(\xe4\xb8\xb0\xe5\xaf\x8c\ ...

  9. Python爬虫 requests库基础

    requests库简介 requests是使用Apache2 licensed 许可证的HTTP库. 用python编写. 比urllib2模块更简洁. Request支持HTTP连接保持和连接池,支 ...

随机推荐

  1. LG4777 【模板】扩展中国剩余定理(EXCRT)

    题意 题目描述 给定\(n\)组非负整数\(a_i, b_i\),求解关于\(x\)的方程组 \[\begin{cases} x \equiv b_1\ ({\rm mod}\ a_1) \\ x\e ...

  2. ambassador 学习六 Module说明

    模块允许给与特定的mapping 或者整体添加特定的行为,方便进行系统的控制. 当前的module 定义主要是系统级别的 当前系统主要的配置 --- apiVersion: ambassador/v0 ...

  3. android高速上手(三)经常使用控件使用

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wangpeifeng669/article/details/26288387 完毕了android的 ...

  4. javaweb经典面试题

    1.hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得java程序员可以随心所欲的使用对象编程思维来操纵数据库. 工作原理: 1.读取并解析配置文件2. ...

  5. hadoop YARN配置参数剖析—MapReduce相关参数

    MapReduce相关配置参数分为两部分,分别是JobHistory Server和应用程序参数,Job History可运行在一个独立节点上,而应用程序参数则可存放在mapred-site.xml中 ...

  6. windows上如何安装Sqlite

    一.安装 下载地址:http://www.sqlite.org/download.html 将Precompiled Binaries for Windows下的包下载下来sqlite-dll-win ...

  7. windows 内存分配回收检查工具

    LeakDiag是微软一款检测memory leak的工具,使用比较简单 首先去下载一个ftp://ftp.microsoft.com/PSS/Tools/Developer%20Support%20 ...

  8. linux 内核调试之关键函数名记要

    gdbserver + gdb 调试内核 记到函数名,其它就能用gdb看了 start_kernel 内核启动 run_init_process    init进程启动 主要是根据shell脚本初始化 ...

  9. IDEA中修改各个部位的字体大小

    1.菜单栏 Setting -> Appearance&Behavior -> Appearance ->Override default fonts by (not rec ...

  10. c#中变量的作用域

    C#中的作用域和javascript中的作用域还是有区别的.呵呵 class Person { /* *确定C#变量作用域的2个规则. 1.类的字段所处的作用域等同于该字段所属类所在的作用域; * 2 ...