python的requests用法详解
python接口测试Requests用法详解
Requests是一个Python语言写的http相关设置或者请求的一个库
安装:pip install Requests或者pip3 install requests
使用的时候要import requests
http://httpbin.org/:这个链接可以用来检查你请求的链接返回的内容,输出的是你请求的基本内容,可以用来测试验证
get请求
1.基本get请求的写法:
import requests
response = requests.get("http://httpbin.org/get")
print(response.text)
打印出你的请求头,请求链接,IP地址等
2.带参数的get请求(在链接的后面加上问号再加参数):
Response = requests.get("http://httpbin.org/get?name=germey&age=22")
# 还有一种方式:就是先定义一个字典,然后调用get方法的时候,将字典赋值给给params参数
data = {
‘name’:’germey’,
‘age’:22
}
response = requests.get("http://httpbin.org/get",params=data)
print(response.text)
3.解析json
Json用来保存一些键值对组成的数据,用于数据交换,也可用于前后端之间互相传递数据,比如前端发起请求,调用接口,后端返回一串json数据,处理数据,渲染到页面上。
Request模块中也有解析json的方法:
import requests
response = requests.get("http://httpbin.org/get")
print(type(response.text))
print(response.json())
print(type(response.json()))
还可以这样写:
Import requests
Import json
response = requests.get("http://httpbin.org/get")
#和request.json()是一个意思,都是打印出一个字典数据,这个json()方法也是调用了json的loads方法
print(json.loads(requests.test()))
4.获取二进制数据
一般用来下载图片、视频等
response = requests.get("http://github.com/favicon.ico") #将要下载的图片链接放这
print(type(response.text),type(response.content)) #类型分别是str和bytes
print(response.text)
print(response.content) #获取二进制数据的方法
图片的二进制数据获取到后怎么保存呢?
import requests
response = requests.get("http://github.com/favicon.ico")
with open("favicon.ico","wb") as f:
f.write(response.content)
f.close()
5.添加headers
有时候不加headers发请求的时候会直接被拒绝或者服务器错误等,加上headers就可以了。
比如下面这段会报500错误:
response = requests.get("https://www.zhihu.com/expiore")
print(response.text)
此时需要添加一个headers:
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36"
}
response = requests.get("https://www.zhihu.com/expiore",headers=headers)
print(response.text)
基本post请求,和get请求类似
6.需要的表单数据也用一个字典存起来给data参数,headers和get方法一样
Import requests
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36"
}
data = {
‘name’:’germey’,
‘age’:22
}
# 需要注意和get方法不同的是如果有参数用的是data=而不是params=
response = requests.post("https://www.zhihu.com/expiore",data = data,headers = headers)
print(response.json())
响应
7.response属性
response = requests.post("https://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) #历史记录
8.状态码判断
response = requests.post("https://www.jianshu.com")
exit() if not response.status_code==200 else print("request sucessfully")
或者可以写成:
exit() if not response.status_code==request.codes.ok else print("request sucessfully")
状态码和request.codes列表中的英文状态对应表
|
状态码 |
英文 |
|
100 |
("continue",) |
|
101 |
("switching_protocols") |
|
102 |
("processing",) |
|
103 |
("checkpoint",) |
|
122 |
("url_too_long","request_url_too_long") |
|
200 |
("ok","okay","all_ok","all_good",\\o/,"√") |
|
201 |
("created",) |
|
202 |
("accepted",) |
|
203 |
("non_authoritative_info","non_authoritative_information") |
|
204 |
("no_content",) |
|
205 |
("reset_content","reset") |
|
206 |
("partial_content","partial") |
|
207 |
("multi_status","multiple_status","multi_stati","multiple_stati") |
|
208 |
("already_reported",) |
|
300 |
("multiple_choices",) |
|
301 |
("moved_permanentiy","moved","\\o-") |
|
302 |
("found",) |
|
303 |
("see_other","other") |
|
304 |
("not_modified") |
|
305 |
("use_proxy") |
|
306 |
("switch_proxy") |
|
307 |
("temporary_redirect","remporary_moved","temporary") |
|
308 |
("permanent_redirect","resume_incomplete","resume") |
|
400 |
("bad_request","bad") |
|
401 |
("unauthorized",) |
|
402 |
("payment_required","payment") |
|
403 |
("forbidden",) |
|
404 |
("not_found","-o-") |
|
405 |
("method_not_allowed","not_allowed") |
|
406 |
("not_acceptable",) |
|
407 |
("proxy_authentication_required","proxy_auth","proxy_authentication") |
|
408 |
("request_timeout","timeout") |
|
409 |
("conflict",) |
|
410 |
("gone",) |
|
411 |
("length_required",) |
|
412 |
("precondition_falled","precondition") |
|
413 |
("request_entity_too_large",) |
|
414 |
("request_uril_too_large",) |
|
415 |
("unsupported_media_type","unsupported_media","media_type") |
|
416 |
("requested_range_not_satisfiable"," requested_range"," range_not_satisfiable") |
|
417 |
("expectation_failed",) |
|
418 |
("im_a_teapot","teapot","i_am_a_teapot") |
|
421 |
("misdirected_request",) |
|
422 |
("unprocessable_entity","unprocessable") |
|
423 |
("locked",) |
|
424 |
("failed_dependency","dependency") |
|
425 |
("unordered_collection","unordered") |
|
426 |
("upgrade_required","upgrade") |
|
428 |
("precondition_required","precondition") |
|
429 |
("too_many_requests","too_mary") |
|
431 |
("header_fields_too_large"," fields_too_large") |
|
444 |
("no_response","none") |
|
449 |
("retry_with","retry") |
|
450 |
("unavallable_for_legal_reasons","parental_controls") |
|
451 |
("unavallable_for_legal_reasons","legal_reasons") |
|
499 |
("client_closed_request",) |
|
500 |
("internal_server_error","server_error","/o\\","×") |
|
501 |
("not_implemented",) |
|
502 |
("bad_gateway",) |
|
503 |
("service_unavaliable","unavaliable") |
|
504 |
("gateway_timeout") |
|
505 |
("http_version_not_supported","http_version") |
|
506 |
("variant_also_negotiates",) |
|
507 |
("insufficient_storage",) |
|
509 |
("bandwidth_limit_exceeded","bandwidth") |
|
510 |
("not_extended",) |
|
511 |
("network_authentication_required","network_auth","network_authentication") |
request的高级操作
文件上传
import requests
files = {"file":open("favicon",rb)}
response = requests.post("http://httpbin.org/post",files=files) #需要用post方法
print(response.text)
text返回的files是一个文件字节流
获取cookie
response = requests.get("http://www.baidu.com")
print(response.cookies)
for key,value in response.cookies.items():
print(key + "=" + value)
会话维持:用来模拟登录
requests.get("http://httpbin.org/cookies/set/number/1234") #调用cookies的set方法设置cookies
response = requests.get("http://httpbin.org/cookies") #获取cookies
print(response.cookies)
这样结果出来是空的,因为你请求了两次get方法,等于是两个浏览器各自请求
修改一下:
s = requests.Session()
s.get("http://httpbin.org/cookies/set/number/1234")
response = s.get("http://httpbin.org/cookies")
print(response.text)
证书验证
#这个12306的证书是一个不安全的,直接请求会报错:SSL error证书错误
response = requests.get("http://www.12306.cn")
# 这样改之后就返回200ok了,但是会有警告
response = requests.get("http://www.12306.cn",verfiy=False)
再修改:
from requests.packages import urllib3
urllib3.disable_warnings()
response = requests.get("http://www.12306.cn",verfiy=False)
print(response.text)
这样就没有警告了
代理设置
设置http或者https代理,并且没有用户名密码的情况下:
Import requests
proxies = {
"http":"http://127.0.0.1:9743/",
"https":"https://127.0.0.1:9743/"
}
response = requests.get("https://www.taobao.com",proxies=proxies)
print(response.status_code)
设置http或者https代理,有用户名密码
proxies = {
"http":" http://user:password@ 127.0.0.1:9743/"
}
response = requests.get("https://www.taobao.com",proxies=proxies)
print(response.status_code)
如果代理不是http或者https,是socks4或者socks5,则字典的键值还是http,但是值变了:
首先需要 pip install "requests[socks]"
Import requests
proxies = {
"http":"socks5://127.0.0.1:9743/",
"https":"socks5://127.0.0.1:9743/"
}
response = requests.get("https://www.taobao.com",proxies=proxies)
print(response.status_code)
超时设置
import requests
response = requests.get("https://www.taobao.com",timeout=1) #设置超过1秒就算超时
print(response.status_code)
超时以后会报一个异常:ReadTimeout
可以使用try捕获异常,修改代码为:
Import requests
from requests.exceptions import ReadTimeout
try:
response = requests.get("https://www.taobao.com",timeout=1) #设置超过1秒就算超时
print(response.status_code)
except ReadTimeout:
print("timeout")
认证设置
有写网址需要用户名密码登录后才可以看到内容,则:
import requests
from requests.auth import HTTPBasicAuth
r = requests.get("http://120.27.34.24:9001",auth=HTTPBasicAuth("user","123"))
print(r.status_code)
或者第二种写法:
Import requests
r=requests.get("http://120.27.34.24:9001",auth= ("user","123"))
print(r.status_code)
- 异常处理
import requests from requests.exceptions import ReadTimeout,HTTPError,ConnectionError,RequestException try:
response = requests.get("https://www.taobao.com",timeout=1)
print(response.status_code)
except ReadTimeout:
print("timeout")
except HTTPError: #http异常
print("http error")
except ConnectionError: #网络不好
print("ConnectionError")
except RequestException: #父类异常
print("error")
python的requests用法详解的更多相关文章
- python操作redis用法详解
python操作redis用法详解 转载地址 1.redis连接 redis提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用 ...
- 【python】self用法详解
在介绍Python的self用法之前,先来介绍下Python中的类和实例我们知道,面向对象最重要的概念就是类(class)和实例(instance). 类是抽象的模板,比如学生这个抽象的事物,可以用一 ...
- python的requests库详解
快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Req ...
- Python中enumerate用法详解
enumerate()是python的内置函数.适用于python2.x和python3.xenumerate在字典上是枚举.列举的意思enumerate参数为可遍历/可迭代的对象(如列表.字符串)e ...
- Python:requests:详解超时和重试
网络请求不可避免会遇上请求超时的情况,在 requests 中,如果不设置你的程序可能会永远失去响应.超时又可分为连接超时和读取超时. 连接超时 连接超时指的是在你的客户端实现到远端机器端口的连接时( ...
- python sorted排序用法详解
sorted排序 python sorted 排序 1. operator函数在介绍sorted函数之前需要了解一下operator函数. operator函数是python的内置函数,提供了一系列常 ...
- Python中with用法详解
一 .with语句的原理 上下文管理协议(Context Management Protocol):包含方法 __enter__()和__exit__(),支持该协议的对象要实现这两个方法. 上下文管 ...
- Python 注释(Python Comments)用法详解
目录 1 Python 注释概述 2 Python 注释的作用 2.1 调试代码 2.2 提高程序的可读性 3 Python 单行注释 3.1 Python 单行注释概述 3.2 单行注释注释单行代码 ...
- Python中第三方库Requests库的高级用法详解
Python中第三方库Requests库的高级用法详解 虽然Python的标准库中urllib2模块已经包含了平常我们使用的大多数功能,但是它的API使用起来让人实在感觉不好.它已经不适合现在的时代, ...
随机推荐
- python基础数据类型汇总
list和dict 在循环一个列表和字典时,最好不要删除其中的元素,这样会使索引发生改变,从而报错! lis = [11, 22, 33, 44, 55] for i in range(len(lis ...
- springboot中使用spring-session实现共享会话到redis(二)
上篇文章介绍了springboot中集成spring-session实现了将session分布式存到redis中.这篇在深入介绍一些spring-session的细节. 1.session超时: 在t ...
- sci,ei,istp三大科技文献检索系统
印刷版(SCI) 双月刊 ,500种 联机版(SciSearch) 周更新 ,600种 光盘版(带文摘)(SCICDE) 月更新 ,500种(同印刷版) 网络版(SCIExpanded) 周更新 ,6 ...
- HDU 3746 Cyclic Nacklace(kmp next数组运用)
Cyclic Nacklace Problem Description CC always becomes very depressed at the end of this month, he ha ...
- LightOJ - 1265 Island of Survival (概率dp)
You are in a reality show, and the show is way too real that they threw into an island. Only two kin ...
- python数据分析经常使用的库
这个列表包含数据分析经常使用的Python库,供大家使用.1. 网络通用urllib -网络库(stdlib).requests -网络库.grab – 网络库(基于pycurl).pycurl – ...
- 力扣90——子集 II
原题 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [1,2,2], ...
- Webpack4不求人系列(1)
Webpack是一个现在Javascript应用程序的模块化打包器,在Webpack中JS/CSS/图片等资源都被视为JS模块,简化了编程.当Webpack构建时,会递归形成一个模块依赖关系图,然后将 ...
- css3新增属性有哪些?css3中常用的新增属性总结
css3新增属性有哪些?来提问这个问题的人都应该知道css3是css的升级版本,那么,css3既然是升级版本,自然是会新增一些属性,接下来本篇文章将给大家介绍关于css3中常用的新增属性. 一.css ...
- 【转载】CSS filter:hue-rotate色调旋转滤镜实现按钮批量生产
文章转载自 张鑫旭-鑫空间-鑫生活 http://www.zhangxinxu.com/ 原文链接:https://www.zhangxinxu.com/wordpress/2018/11/css-f ...