requests

实例引入

  1. import requests
  2. response = requests.get('https://www.baidu.com')
  3. response.status_code
  4. response.text
  5. response.cookies

请求方式

  1. post()
  2. put()
  3. delete()
  4. head()
  5. options()

请求

基本get请求

带参数get请求

  1. data = {'name':'germey', 'age':'22}
  2. response = request.get('http://httpbin.org/get', params=data)
  3. print(respones.text)

解析json

  1. response.json()

获取二进制数据

  1. response.content
  1. response=request.get('https://github.com/favicon.ico')
  2. f = open('favicon.ico', 'wb')
  3. f.write(response.content)
  4. f.close()

添加headers

  1. headers={
  2. 'User-Agent':'Mozilla/5.0 (Macintosh; intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
  3. }
  4. response = request.get('https://www.zhihu.com/explore'. headers=headers_

POST请求

  1. data = {}
  2. headers = {}
  3. response = request.post('http://httpbin.org/post', data=data, headers=headers)

response属性

  1. status_code
  2. headers
  3. cookies
  4. url
  5. history

高级操作

文件上传

  1. files = {'file':open('favicon.ico','rb')}
  2. response = request.get('http://httpbin.org/post', files=files)

获取cookie

  1. for key,value in response.cookies.items():
  2. print(key + '=' + value)

会话维持

  1. requests.get('http://httpbin.org/cookies/set/number/123456789)
  2. response = requests.get('http://httpbin.org/cookies')

上述方法无法得到想要的cookie

  1. s = requests.Session()
  2. s.get(...)
  3. response = s.get(...)

证书验证

暂时不看。如果发生情况则添加参数 verify=False

代理设置

  1. proxies={}
  2. response = requests.get(' ', proxies=proxies)

超时设置

  1. from requests.exceptions import ReadTimeout
  2. try:
  3. #some codes
  4. except ReadTimeout:
  5. print('Timeout')

认证设置

  1. request.get(...,auth={'user','123'})

异常处理

爬虫二之Requests的更多相关文章

  1. Python 爬虫二 requests模块

    requests模块 Requests模块 get方法请求 整体演示一下: import requests response = requests.get("https://www.baid ...

  2. 爬虫二 requests模块的使用

    一.requests模块的介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:reques ...

  3. 爬虫学习(二)requests模块的使用

    一.requests的概述 requests模块是用于发送网络请求,返回响应数据.底层实现是urllib,而且简单易用,在python2.python3中通用,能够自动帮助我们解压(gzip压缩的等) ...

  4. 爬虫四大金刚:requests,selenium,BeautifulSoup,Scrapy

    一.简介爬虫 1.什么是爬虫 #1.什么是互联网? 互联网是由网络设备(网线,路由器,交换机,防火墙等等)和一台台计算机连接而成,像一张网一样. #2.互联网建立的目的? 互联网的核心价值在于数据的共 ...

  5. 【python网络爬虫】之requests相关模块

    python网络爬虫的学习第一步 [python网络爬虫]之0 爬虫与反扒 [python网络爬虫]之一 简单介绍 [python网络爬虫]之二 python uillib库 [python网络爬虫] ...

  6. 爬虫开发5.requests模块的cookie和代理操作

    代理和cookie操作 一.基于requests模块的cookie操作 引言:有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个人主页数据)时,如果使用之前requests ...

  7. 爬虫系列(七) requests的基本使用

    一.requests 简介 requests 是一个功能强大.简单易用的 HTTP 请求库,可以使用 pip install requests 命令进行安装 下面我们将会介绍 requests 中常用 ...

  8. 爬虫中之Requests 模块的进阶

    requests进阶内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 引入 有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个 ...

  9. 网络爬虫入门:你的第一个爬虫项目(requests库)

    0.采用requests库 虽然urllib库应用也很广泛,而且作为Python自带的库无需安装,但是大部分的现在python爬虫都应用requests库来处理复杂的http请求.requests库语 ...

随机推荐

  1. PIL 中的 Image 模块

    转载:http://www.cnblogs.com/way_testlife/archive/2011/04/20/2022997.html   PIL 中的 Image 模块 本文是节选自 PIL ...

  2. php晚了8小时 PHP5中的时间相差8小时的解决办法

    php页面顶部加一句date_default_timezone_set("Asia/Shanghai");或者直接在php.ini设置date.timezone=Asia/Shan ...

  3. nasm

    sudo apt install nasm 64bit: nasm -f elf64 test.asm ld -s -o test test.o --------------------------- ...

  4. java多线程面试题整理及答案

    1) 什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序员可以通过它进行多处理器编程,你可以使用多线程对 运算密集型任务提速.比如,如果一个线程完 ...

  5. Tronado【第2篇】:tronado自定义Form组件

    Tronado自定义Form组件 一.获取类里面的静态属性以及动态属性的方法 方式一: # ===========方式一================ class Foo(object): user ...

  6. Collections 工具类和 Arrays 工具类常见方法

    Collections Collections 工具类常用方法: 排序 查找,替换操作 同步控制(不推荐,需要线程安全的集合类型时请考虑使用 JUC 包下的并发集合) 排序操作 void revers ...

  7. shell练习--PAT题目1008:数组元素循环右移问题 (失败案例,运行超时)

    一个数组A中存有N(>)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥)个位置,即将A中的数据由(A​0​​A​1​​⋯A​N−1​​)变换为(A​N−M​​⋯A​N−1​​A ...

  8. shiro框架学习-2-springboot整合shiro及Shiro认证授权流程

    1. 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  9. SQL 以逗号分隔查询;调用自定义函数

    select col from [dbo].[GetInPara]('101,102,103',',') USE [xxx] GO /****** Object: UserDefinedFunctio ...

  10. 虚拟机使用桥接模式连接网络并且设置静态ip

    1.桥接模式连接网络 虚拟机连接网络一共有四种模式,我这里只介绍桥接模式,毕竟坑了我几个小时 设置有线连接,我本来用的无线连接完成微信点餐系统,后来换了有线因为有线连接不会分配ip,和本地电脑使用同一 ...