什么是Requests

Requests是用python语言编写,基于urllib,采用Apache2 licensed开源协议的HTTP库,它比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求。
一句话总结:它是Python实现的简单易用的HTTP库 安装Requests pip install requests

验证没有报错,表示已经成功的安装了

实例引入
 import requests
response = requests.get('https://www.baidu.com')
print(type(response))
print(response.status_code) # 状态码
print(type(response.text))
print(response.text) # 响应的内容
print(response.cookies) # 获取cookie

各种请求方式
 import requests
print(requests.post('http://httpbin.org/post')) #
print(requests.put('http://httpbin.org/put'))
print(requests.delete('http://httpbin.org/delete'))
print(requests.head('http://httpbin.org/get'))
print(requests.options('http://httpbin.org/get'))
Requests库请求具体怎么用的
基本GET请求-----------------------------------------------------------------------------------------------------------
基本写法
 import requests

 response = requests.get('http://httpbin.org/get')
print(response.text) # 请求头啊,请求的IP地址,请求的链接
带参数的GET请求
 import requests

 response = requests.get('http://httpbin.org/get?name=germey&age=22')
print(response.text)
也可以用字典的形式传参 params
 import requests

 data = {
'name':'xiaohu',
'age':''
}
response = requests.get('http://httpbin.org/get',params=data)
print(response.text)
解析json
 import requests

 response = requests.get('http://httpbin.org/get')
print(type(response.text))
print(response.json())
print(type(response.json())) # 它是一个字典的类型
区别用json.loads与直接.json有什么不一样,结果其实是一样的
 import requests
import json response = requests.get('http://httpbin.org/get')
print(type(response.text))
print(response.json())
print(json.loads(response.text)) #区别用json.loads与直接.json有什么不一样,结果其实是一样的
print(type(response.json())) # 它是一个字典的类型
获取二进制数据
 import requests

 response = requests.get('https://github.com/favicon.ico')
print(type(response.text),type(response.content)) # .content是二进制内容
print(response.text)
print(response.content)
获取到图片把它保存
  import requests
response = requests.get('https://github.com/favicon.ico')
with open('favicon','wb') as f:
f.write(response.content)
f.close()
 添加headers 如果不加headers 报错500
import requests 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 = requests.get("https://www.zhihu.com/explore",headers=headers)
print(response.text)
基本POST请求
 import requests

 data = {
'name':'xiaohu',
'age':'',
'job':'IT'
}
response = requests.post('http://httpbin.org/post',data=data)
print(response.text)
 import requests

 data = {
'name':'xiaohu',
'age':'',
'job':'IT'
}
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 = requests.post('http://httpbin.org/post',data=data,headers=headers)
print(response.json())
响应
response属性
 import requests

 response = requests.get('http://www.jianshu.com')
print(type(response.status_code),response.status_code) # 状态码
print(type(response.headers),response.headers) # 请求头
print(type(response.cookies),response.cookies) #
print(type(response.url),response.url)
print(type(response.history),response.history) # 访问的历史记录
高级操作
文件上传
 import requests

 files = {
'file':open('favicon','rb')
}
response = requests.post('http://httpbin.org/post',files=files)
print(response.text)
获取cookie
 import requests

 response = requests.get('https://www.baidu.com')
print(response.cookies)
print(type(response.cookies))
for key,value in response.cookies.items():
print(key+'='+value)
会话维持
 import requests
requests.get('http://httpbin.org/cookies/set/number/1165872335') # 为网站设置一个cookies
response = requests.get('http://httpbin.org/cookies') # 再用get访问这个cookies
print(response.text) # 为空,因为这里进行了两次get请求,相当于两个浏览器分别设置cookies和访问cookies,相对独立的
改进
 import requests
S = requests.Session() # 声明对象
S.get('http://httpbin.org/cookies/set/number/1165872335') # 实现在同一个浏览器进行设置rookies和访问rookies
response = S.get('http://httpbin.org/cookies') # 再用get访问这个cookies
print(response.text) # 此时不为空
证书验证
 import requests
from requests.packages import urllib3
urllib3.disable_warnings() # 消除警告信息 response = requests.get('https://www.12306.cn',verify=False) # verify=False 不需要验证进入,但是有警告
print(response.status_code)
 import requests

 response = requests.get('https://www.12306.cn',cert=('/path/server.crt','/path/key')) # 指定的证书
print(response.status_code)
代理设置
 import requests
import socks5 proxies = {
"http":"socks5://127.0.0.1:8080",
"https":"socks5://127.0.0.1:8080",
} response = requests.get('https://www.taobao.com',proxies=proxies)
print(response.status_code)
超时设置
 import requests
from requests.exceptions import ReadTimeout try:
response = requests.get('https://www.httpbin.org/get',timeout = 0.2)
print(response.status_code)
except ReadTimeout:
print("timeout")
认证设置
 import requests
from requests.auth import HTTPBasicAuth r = requests.get('http://127.27.34.24:9001',auth=HTTPBasicAuth('user',''))
print(r.status_code) # 第二种方式
import requests r = requests.get('http://127.27.34.24:9001',auth=('user',''))
print(r.status_code)
异常处理
import requests
from requests.exceptions import ReadTimeout,HTTPError,RequestException,ConnectionError
try:
response = requests.get('http://httpbin.org/get',timeout = 0.2)
print(response.status_code)
except ReadTimeout:
print('Timeout')
except HTTPError:
print('Http error')
except ConnectionError:
print('Connection Error')
except RequestException:
print('error')
												

爬虫学习--Requests库详解 Day2的更多相关文章

  1. Python爬虫:requests 库详解,cookie操作与实战

    原文 第三方库 requests是基于urllib编写的.比urllib库强大,非常适合爬虫的编写. 安装: pip install requests 简单的爬百度首页的例子: response.te ...

  2. Python爬虫系列-Requests库详解

    Requests基于urllib,比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求. 实例引入 import requests response = requests.get( ...

  3. Python爬虫系列-Urllib库详解

    Urllib库详解 Python内置的Http请求库: * urllib.request 请求模块 * urllib.error 异常处理模块 * urllib.parse url解析模块 * url ...

  4. Python爬虫学习==>第八章:Requests库详解

    学习目的: request库比urllib库使用更加简洁,且更方便. 正式步骤 Step1:什么是requests requests是用Python语言编写,基于urllib,采用Apache2 Li ...

  5. python WEB接口自动化测试之requests库详解

    由于web接口自动化测试需要用到python的第三方库--requests库,运用requests库可以模拟发送http请求,再结合unittest测试框架,就能完成web接口自动化测试. 所以笔者今 ...

  6. python接口自动化测试之requests库详解

    前言 说到python发送HTTP请求进行接口自动化测试,脑子里第一个闪过的可能就是requests库了,当然python有很多模块可以发送HTTP请求,包括原生的模块http.client,urll ...

  7. requests库详解 --Python3

    本文介绍了requests库的基本使用,希望对大家有所帮助. requests库官方文档:https://2.python-requests.org/en/master/ 一.请求: 1.GET请求 ...

  8. python的requests库详解

    快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Req ...

  9. 爬虫学习--http请求详解

    上篇博客里面写了,爬虫就是发http请求(浏览器里面打开发送的都是http请求),然后获取到response,咱们再从response里面找到想要的数据,存储到本地. 咱们本章就来说一下什么是http ...

随机推荐

  1. 对BFC规范的理解

    什么是BFC? BFC 全称为 块级格式化上下文(Block Fromatting Context),是Web页面的可视化CSS渲染出的一部分.它是块级盒布局出现的区域,也是浮动层元素进行交互的区域. ...

  2. python编程基础之七

    运算关系:也就是常说比较运算,返回值只有True, False ==  判断是否相等 != 判断是否不相等 > ,< ,>= , <=    判断是否大于,小于,大于等于,小于 ...

  3. BF算法(蛮力匹配)

    输入主串a,模式b b在a中的位置 1.在串a和串b中设置比较的下标i=0,j=0: 2.重复下述操作,直到a或b的所有字符均比较完毕: 2.1如果a[i]等于b[i],继续比较a和b的下一对字符: ...

  4. ANC主动降噪理论

    根据系统是否有参考信号传感器可将ANC系统大致的分为前馈型和反馈型. 前馈控制是产生次级噪声之前就通过传感器测量初级噪声的频率以获取参考信号. 反馈控制不需要测得参考信号就产生次级噪声进行相消干涉 反 ...

  5. windows显示文件后缀名

    win+E 进入到计算机 点击组织 点击文件夹和搜索选项 先点击查看,然后去掉勾选隐藏已知文件类型的扩展名

  6. Zabbix 2.2系列注入+getsehll

    Zabbix 是一个开源的企业级性能监控解决方案. 官方网站:http://www.zabbix.com Zabbix 的jsrpc的profileIdx2参数存在insert方式的SQL注入漏洞,攻 ...

  7. 代码审计-(Ear Music).任意文件下载漏洞

    0x01 代码分析 后台地址:192.168.5.176/admin.php admin admin 安装后的界面 在后台发布了一首新歌后,前台点进去到一个“下载LRC歌词”功能点的时候发现是使用re ...

  8. POWERSPLOIT-Recon(信息侦察)脚本渗透实战

    Recon(信息侦察)模块 a) 调用invoke-Portscan扫描内网主机的端口. 1)通过IEX下载并调用invoke-portscan. PS C:\Users\Administrator& ...

  9. 在VS2013下配置BOOST库

    1.安装Boost库 (1).首先打开Boost的官网(http://www.boost.org/),找到下载位置,如下图中红框所示,此时最新的版本是1.64.0: (2).点击进入下载页面,选择你需 ...

  10. vue MD5 加密

    确保vue项目中有MD5的依赖,当然没有的可以安装crypto模块. npm安装: npm install --save crypto 在main.js文件中将md5引入,可以全局使用的 import ...