requests

实例引入

import requests

response  = requests.get('https://www.baidu.com')
response.status_code
response.text
response.cookies

请求方式

post()
put()
delete()
head()
options()

请求

基本get请求

带参数get请求

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

解析json

response.json()

获取二进制数据

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

添加headers

headers={
'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'
}
response = request.get('https://www.zhihu.com/explore'. headers=headers_

POST请求

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

response属性

status_code
headers
cookies
url
history

高级操作

文件上传

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

获取cookie

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

会话维持

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

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

s = requests.Session()
s.get(...)
response = s.get(...)

证书验证

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

代理设置

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

超时设置

from requests.exceptions import ReadTimeout

try:
#some codes
except ReadTimeout:
print('Timeout')

认证设置

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. SQL注入--反引号

    反引号是个比较特别的字符,下面记录下怎么利用 0x00 SQL注入 反引号可利用在分隔符及注释作用,不过使用范围只于表名.数据库名.字段名.起别名这些场景,下面具体说下 1)表名 payload:se ...

  2. Codeforces 980 并查集/模拟贪心最小字典序 找规律/数去除完全平方因子 逆思维倍增预处理祖先标记点

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...

  3. 大数据(量上GB的)查看命令

    大数据查看指令 cmd中(tab键可补全) type 文件名:全部查看 more 文件名:Enter键,按一下查看一页

  4. java数据结构--array与ArrayList的区别

    ArrayList 内部是由一个array 实现的. 如果你知道array 和 ArrayList 的相似点和不同点,就可以选择什么时候用array 或者使用ArrayList , array 提供 ...

  5. 【LuoguP5289】[十二省联考2019] 皮配

    题目链接 题目描述 略 Sol 一道背包问题 首先暴力做法设 \(dp[i][j][k]\) 表示前 \(i\) 个城市的学校被分到第一阵营 \(j\) 人 第一门派 \(k\) 人的方案数. 中间一 ...

  6. docker安装各类软件

    安装Docker Docker 要求 CentOS 系统的内核版本高于 3.10 , uname -r 命令查看你当前的内核版本 1 安装一些必要的系统工具: sudo yum install -y ...

  7. Docker(四):Docker常用命令

    除过以上我们使用的Docker命令外,Docker还有一些其它常用的命令 拉取docker镜像 docker pull image_name 查看宿主机上的镜像,Docker镜像保存在/var/lib ...

  8. Python如何快速初始化一个二维数组

    正确的方法 cols= 3 rows = 2 memo = [[1] * m for i in range(n) ] 下面这种做法是错误的 memo = [[1] * m ] * n 这会导致 mem ...

  9. PHP入门培训教程 PHP变量及常量

         一.PHP5.4的基本语法格式 1.PHP的分割符 $php=true; //分号结束语句 if($php){ echo "真"; //分号结束语句 } //大括号结束语 ...

  10. JPA学习(四、JPA_映射关联关系)

    框架学习之JPA(四) JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中 ...