01.Requests库学习
来源:互联网 发布:微山湖武工队知乎 编辑:程序博客网 时间:2024/06/03 20:42
Requests库学习
Requests介绍
requests库是一个Python的HTTP库,可以进行丰富的Http操作。
Requests安装
pip install requests
常用方法介绍
requests.request(method, url, **args)
构造一个请求,是requests其他库的基础方法,其他请求方法都是在request方法之上构造的。
method
参数包含GET、 POST、 HEAD、 PUT、 PATCH、 DELETE url
参数是URL args
是请求的配置参数包含如下:
params
参数拼接到GET请求的url后的参数 格式使用dict类型
payload = {'key1': 'value1', 'key2': ['value2', 'value3']}r = requests.request('GET','http://httpbin.org/get', params=payload)print(r.url)'''http://httpbin.org/get?key1=value1&key2=value2&key2=value3'''
headers
请求头信息,dict类型
url = 'https://api.github.com/some/endpoint'headers = {'user-agent': 'my-app/0.0.1'}r = requests.request('GET',url, headers=headers)
data
配合POST请求,data传递请求体参数
payload = {'key1': 'value1', 'key2': 'value2'}r = requests.post("http://httpbin.org/post", data=payload)
json
参数,配合POST请求发送请求体,会转换成JSON格式数据
url = 'https://api.github.com/some/endpoint'payload = {'some': 'data'}r = requests.post(url, json=payload)
files
参数,上传文件
url = 'http://httpbin.org/post'files = {'file': open('report.xls', 'rb')}r = requests.post(url, files=files)
cookies
参数,设置请求cookies
url = 'http://httpbin.org/cookies'cookies = dict(cookies_are='working')r = requests.get(url, cookies=cookies)
allow_redirects
参数, 是否开启重定向
r = requests.get('http://github.com', allow_redirects=False)#不允许重定向
timeout
参数,设置超时时间,timeout 仅对连接过程有效
requests.get('http://github.com', timeout=0.001)
proxies
设置代理
pxs = {'http':'http://user:pass@xxx', 'https':'xxxxx'}r = requests.request("GET",url, proxies=pxs)
auth
: 元组,支持HTTP认证功能 stream
: True/False,默认为True,获取内容立即下载开关 verify
: True/False,默认为True,认证SSL证书开关 cert
: 本地SSL证书路径
requests.get(url, params=None,**args)
get函数就是对request的在封装,进行get请求
def get(url, params=None, **args): requests.request("GET", url, params, **args)
requests.post(url, data=None,**args)
post函数也是对request封装,进行post请求
requests.head(…)
获取头部信息
requests.put(…)
put请求
requests.patch(…)
提交局部修改,节省流量
requests.delete(…)
delete请求
Response对象介绍
上述请求方法返回值都是Response对象
r = requests.get("http://www.baidu.com")type(r)'''<class 'requests.models.Response'>'''dir(r)'''['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'iter_content', 'iter_lines', 'json', 'links', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code', 'text', 'url']'''
着重介绍如下Response属性: url
:访问的URL status_code
:状态码 reason
:返回结果,状态码后边的值 HTTP/1.1 200 OK encoding
: charset字段的值,默认IOS-8859-1 apparent_encoding
:从内容分析到的编码,可以辅助encoding
:字段 cookies
:cookies headers
:响应头 history
:重定向跳转过的地址 text
:响应体字符串表示 content
:响应体二进制表示 request
:request对象 iter_lines
:成员函数,返回生成器对象,可以按行迭代内容。 raise_for_status
:如果请求返回失败会抛出异常,如果发送了一个错误请求(一个 4XX 客户端错误,或者 5XX 服务器错误响应),我们可以通过 Response.raise_for_status() 来抛出异常
bad_r = requests.get('http://httpbin.org/status/404')print(bad_r.status_code)'''404'''bad_r.raise_for_status()'''Traceback (most recent call last): File "requests/models.py", line 832, in raise_for_status raise http_errorrequests.exceptions.HTTPError: 404 Client Error'''
参考文档Requests官方文档 地址:
http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
- 01.Requests库学习
- Requests库学习笔记
- python学习之 requests库
- Python学习之Requests库
- requests学习
- Requests 学习
- Python 第三方 http 库-Requests 学习
- Python 第三方 http 库-Requests 学习
- Python 第三方 http 库-Requests 学习
- Python爬虫库学习笔记-requests
- Pythoon爬虫之requests库学习
- requests库
- requests库
- requests库
- requests库
- Requests库
- Python模块学习 - Requests
- python requests 模块学习
- JVM和Java历史
- leecode 解题总结:345. Reverse Vowels of a String
- 小王的尴尬日常(二)---Openssl 实现国密算法(基础介绍和产生秘钥对)
- 平面上的地图搜索--Java学习笔记(四)
- kmp算法
- 01.Requests库学习
- C# out和ref
- Xcode Git
- 微信小程序客服消息相关(客服按钮修改)
- 大整数取模
- sqoop 2 API 踩坑纪录
- 面试总结
- java 判断一个字符串是否包含某个字符
- B-tree & B+tree & B*Tree 结构浅析