python requests库的简单使用
requests是python的一个HTTP客户端库,跟urllib,urllib2类似,但比urllib,urllib2更加使用简单。
1. requests库的安装
在你的终端中运行pip安装命令即可
pip install requests
使用源码安装
git clone git://github.com/kennethreitz/requests.git
python setup.py install
2. requests发送请求
使用 Requests 发送网络请求
import requestsreq0 = requests.get("https://www.baidu.com")
print (req0)
# 发送一个 HTTP POST 请求
req1 = requests.post("https://http://httpbin.org/post")
# 发送PUT,DELETE,HEAD 以及 OPTIONS 请求
requests.put("http://http://httpbin.org/put")
requests.delete("http://http://httpbin.org/delete")
requests.head("http://http://httpbin.org/get")
requests.options("http://http://httpbin.org/get")
3. 传递URL 参数
Requests 使用 params 关键字参数,以一个字典来提供这些参数
payload = {'key1': 'value1', 'key2': 'value2'}
req2 = requests.get("https://httpbin.org/get",params=payload)
print (req2.url)
# https://httpbin.org/get?key2=value2&key1=value1
# 注:字典里值为 None 的键都不会被添加到 URL 的查询字符串里
将一个列表作为值传入
payload = {'key1': 'value1', 'key2': ['value2', 'value3']}
req3 = requests.get('http://httpbin.org/get', params=payload)
print (req3.url)
# http://httpbin.org/get?key2=value2&key2=value3&key1=value1
4. 响应内容
requests 读取服务器响应的内容
import requests
req4 = requests.get("https://www.baidu.com")
print (req4.text)
Requests 会自动解码来自服务器的内容。大多数 unicode 字符集都能被无缝地解码,使用req4.encoding 属性可以查询编码格式和改变编码类型
>>> req4.encoding
'ISO-8859-1'
>>> req4.encoding = 'utf-8'
>>> req4.encoding
'utf-8'
5. 二进制响应内容
Requests 会自动为你解码 gzip 和 deflate 传输编码的响应数据
print (req4.content)
6. JSON 响应内容
Requests 中也有一个内置的 JSON 解码器,帮助处理 JSON 数据
import requests
req6 = requests.get("https://github.com/timeline.json")
print (req6.json)
# 如果 JSON 解码失败, r.json 就会抛出一个异常。例如,相应内容是 401 (Unauthorized),尝试访问 r.json 将会抛出 ValueError: No JSON object could be decoded 异常
7. 原始响应内容
想获取来自服务器的原始套接字响应,可以访问 r.raw. 不过先要确保在初始请求中设置了 stream=True
req7 = requsets.get('https://github.com/timeline.json',stream = True)
print (req7.raw)
print (req7.raw.read(10))
8. 定制请求头
为请求添加 HTTP 头部,只要简单地传递一个 dict 给 headers 参数就可以了
url = 'https://api.github.com/some/endpoint'
headers = {'user-agent': 'my-app/0.0.1'}
req8 = requests.get(url, headers=headers)
注:注意: 所有的 header 值必须是 string、bytestring 或者 unicode
9. 更加复杂的 POST 请求
发送一些编码为表单形式的数据——非常像一个 HTML 表单。要实现这个,只需简单地传递一个字典给 data 参数。数据字典在发出请求时会自动编码为表单形式
payload = {'key1':'value1','key2':'value2'}
req9 = requests.post("http://httpbin.org/post", data=payload)
print (req9.text)
'''
{
"args": {},
"data": "",
"files": {},
"form": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.6.0 CPython/2.7.5 Linux/3.10.0-327.36.1.el7.x86_64"
},
"json": null,
"origin": "112.35.10.78",
"url": "http://httpbin.org/post"
}
'''
传递一个 string 而不是一个 dict,那么数据会被直接发布出去
import json
url = 'https://api.github.com/some/endpoint'
payload = {'some':'data'}
req10 = requests.post(url,data=json.dumps(payload))
print (req10)
# {"message":"Not Found","documentation_url":"https://developer.github.com/v3"}
10. POST一个多部分编码(Multipart-Encoded)的文件
Requests 使得上传多部分编码文件变得很简单
url = 'http://httpbin.org/post'
files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}
r = requests.post(url, files=files)
r.text
'''
{
...
"files": {
"file": "some,data,to,send\\nanother,row,to,send\\n"
},
...
}
'''
11. 响应状态码
检测响应状态码
req11 = requests.get('http://httpbin.org/get')
req11.status_code
# 200
Requests还附带了一个内置的状态码查询对象
req11.status_code == requests.codes.ok
# True
12. 响应头
查看以一个 Python 字典形式展示的服务器响应头
req11.headers
'''
{'content-length': '311', 'via': '1.1 vegur', 'x-powered-by': 'Flask', 'server': 'meinheld/0.6.1', 'connection': 'keep-alive', 'x-processed-time': '0.000663995742798', 'access-control-allow-credentials': 'true', 'date': 'Fri, 19 May 2017 12:38:25 GMT', 'access-control-allow-origin': '*', 'content-type': 'application/json'}
'''
注: 响应头的字典比较特殊:它是仅为 HTTP 头部而生的。HTTP 头部是大小写不敏感的
13. Cookie
如果响应中包含一些 cookie,你可以快速访问它们
url = 'http://example.com/some/cookie/setting/url'
req13 = requests.get(url)
req13.cookies['example_cookie_name']
# 'example_cookie_name'
发送你的cookies到服务器,可以使用 cookies 参数
url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')
req14 = requests.get(url,cookies=cookies)
print (req14.text)
'''
{
"cookies": {
"cookies_are": "working"
}
}
'''
14. 重定向与请求历史
使用响应对象的 history 方法来追踪重定向
Response.history 是一个 Response 对象的列表,为了完成请求而创建了这些对象。这个对象列表按照从最老到最近的请求进行排序
req15 = requests.get('http://github.com')
print (req15.url)
# https://github.com/
print (req15.status_code)
# 200
print (req15.history)
# [<Response [301]>]
如果使用的是GET、OPTIONS、POST、PUT、PATCH 或者 DELETE,那么可以通过 allow_redirects 参数禁用重定向处理
req16 = requests.get('http://github.com',allow_redirects=False)
print (req16.status_code)
# 301
print (req16.history)
# []
使用了 HEAD,可以启用重定向
req17 = requests.head('http://github.com',allow_redirects=True)
print (req17.url)
# https://github.com/
print (req17.history)
# [<Response [301]>]
15. 超时
requests 在经过以 timeout 参数设定的秒数时间之后停止等待响应
注:timeout 仅对连接过程有效,与响应体的下载无关。 timeout 并不是整个下载响应的时间限制,而是如果服务器在 timeout 秒内没有应答,将会引发一个异常(更精确地说,是在 timeout 秒内没有从基础套接字上接收到任何字节的数据时)
16. 错误与异常
(1) 遇到网络问题(如:DNS 查询失败、拒绝连接等)时,Requests 会抛出一个 ConnectionError 异常。
(2) 如果 HTTP 请求返回了不成功的状态码, Response.raise_for_status() 会抛出一个 HTTPError 异常。
(3) 若请求超时,则抛出一个 Timeout 异常。
(4) 若请求超过了设定的最大重定向次数,则会抛出一个 TooManyRedirects 异常。
(5) 所有Requests显式抛出的异常都继承自 requests.exceptions.RequestException 。
参考链接:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
转载于:https://www.cnblogs.com/xieshengsen/p/6886164.html
python requests库的简单使用相关推荐
- Python requests库核心源码解析
Requests is an elegant and simple HTTP library for Python, built for human beings. Python requests是最 ...
- Python requests库中文乱码问题汇总(编码)
Python requests库中文乱码问题汇总(编码) 在用requests爬取网页时,经常会碰到网页的编码问题导致中文乱码 <dt>ç¹è²æå¡</dt> 这时首先查看页 ...
- python123九宫格输入_使用python PIL库实现简单验证码的去噪方法步骤
字符型图片验证码识别完整过程及Python实现的博主,我的大部分知识点都是从他那里学来的. 想要识别验证码,收集足够多的样本后,首先要做的就是对验证码原始图片进行处理,对验证码识别分类之前,一般包括: ...
- python 中文姓名库,Python Requests库上传文件中文文件名处理方法是什么
使用Python Requests库,提交POST请求上传文件,不支持中文文件名? 使用Python Requests库,向微信服务器上传媒体问题. 当使用英文文件名称,上传时,一切OK. 当下面代码 ...
- Python - Requests库下载图片
Python - Requests库下载图片 import requests# 获取网络图片资源 r = requests.get('https://www.baidu.com/img/bd_logo ...
- 重写python requests库实现自动拼接url
重写python requests库实现自动拼接url 第一步,新建一个HttpSession类,并继承requests.Session 第二步,实现自动拼接URL 第三步,使用自动拼接的URL发送请 ...
- python中requests库的用途-Python.Requests库的基本使用
Requests安装 使用pip安装命令: pip install requests 打开cmd,输入python然后导入requests如果安装成功没有任何提示 如果提示如下则说明安装失败 Impo ...
- python requests库api_python利用requests库进行接口测试的方法详解
前言 之前介绍了接口测试中需要关注得测试点,现在我们来看看如何进行接口测试,现在接口测试工具有很多种,例如:postman,soapui,jemter等等,对于简单接口而言,或者我们只想调试一下,使用 ...
- python requests库详解_python爬虫之路(一)-----requests库详解
requests库 requests库是python实现的最简单易用的http库. requests库的功能详解. 我们可以自然而然地想到这些方法其实就是http协议对资源的操作. 调用request ...
最新文章
- libnet apply method
- GAN模型-分析角度
- js 处理十万条数据_Python数据可视化2018:为什么这么多的库?
- Oracle密码过期及账户解锁的问题
- 为什么用Ghost备份后会有两个文件?
- 如何设置Windows 8开始画面中瓷贴最大行数
- 关闭tensorflow运行时的警告信息1
- 以下内容为Stackoverflow上整理以作纪录
- 经典排序算法(十八)--Proxmap Sort
- CCDA认证的详细综合叙述
- 扫描二维码,下载Android安装包
- 常用触摸屏485通讯引脚及下载口
- 企业python面试题
- 数据结构知识清单简要
- jupyter notebook修改黑色背景和字体大小
- Maya_to_Unity工作流程
- 服务质量(QoS)--网络大典
- java所用到的英语单词_JAVA常用英语单词
- ONLYOFFICE支持wps格式文件(wps,et,dps)的协作
- 使用VPS时的注意事项