requests不是python自带的,使用前需要安装
-
发送请求
HTTP请求类型有GET,POST,PUT,DELETE,HEAD和OPTIONS
使用requests发送请求的方法如下:
>>> import requests >>> r = requests.get("http://httpbin.org/get") #发送GET请求 >>> r = requests.post("http://httpbin.org/post") #发送POST请求 >>> r = requests.put("http://httpbin.org/put") #发送PUT请求 >>> r = requests.delete("http://httpbin.org/delete") #发送DELETE请求 >>> r = requests.head("http://httpbin.org/get") #发送HEAD请求 >>> r = requests.options("http://httpbin.org/get") #发送OPTIONS请求
-
传递URL参数
params参数会对传入的参数进行拼接处理
通常使用params传的参数为字典的格式
>>> import requests >>> payload = {"word":"test","page":11} >>> r = requests.get("http://httpbin.org/get", params=payload) >>> print(r.url) #打印r里的url参数的值 http://httpbin.org/get?word=test&page=11
字典里的值还可以是列表
>>> payload = {"word":"test","page":[1,2,3]} >>> r = requests.get("http://httpbin.org/get", params=payload) >>> print(r.url) http://httpbin.org/get?word=test&page=1&page=2&page=3
字典中的值为None的键将不会被传参数到url里
>>> payload = {"word":"test","page":None} >>> r = requests.get("http://httpbin.org/get", params=payload) >>> print(r.url) http://httpbin.org/get?word=test
params传的参数也可以直接是字符串
>>> payload = "word=test&page=11" >>> r = requests.get("http://httpbin.org/get", params=payload) >>> print(r.url) http://httpbin.org/get?word=test&page=11
-
响应内容
requests能读取服务器响应的内容
>>> r = requests.get("https://www.cnblogs.com/") >>> r.text #获取网页源代码 ‘‘‘此处为网页源代码‘‘‘ >>> r.encoding #查看网页源代码的编码 ‘utf-8‘ >>> r.encoding = ‘GBK‘ #把网页源码的编码改为gbk >>> r.encoding #再调用的时候,发现网页编码变成了gbk了 ‘GBK‘
-
二进制响应内容
对于非文本请求,requests也能用字节的方式来访问请求响应体
>>> r = requests.get("http://p1.ifengimg.com/a/2018_06/75880eeacd0823d_size11_w230_h152.jpg") >>> r.content ‘‘‘此处为bytes类型的图片内容‘‘‘ >>> r.text ‘‘‘一堆乱码‘‘‘
该方式也能用于文本请求,不过返回的结果为bytes类型
>>> r = requests.get("https://www.cnblogs.com/") >>> r.text ‘‘‘此处为文本类型的网页源代码‘‘‘ >>> r.content ‘‘‘此处为bytes类型的网页源代码‘‘‘
-
JSON响应内容
requests中也有一个内置的json解码器,帮助我们处理json数据
>>> import requests >>> r = requests.get("https://github.com/timeline.json") >>> r.json() {‘message‘: ‘Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.‘, ‘documentation_url‘: ‘https://developer.github.com/v3/activity/events/#list-public-events‘} >>> r.status_code 410 >>> r.raise_for_status <bound method Response.raise_for_status of <Response [410]>>
如果json数据解码失败,就会抛出一个ValueError: No JSON object could be decoded的异常
但是成功调用r.json()也不能说明响应成功,有的服务器会在失败的响应中包含一个json对象,如HTTP 500的错误细节,这种json也会被解码返回
所以要检查请求是否成功,可以使用r.status_code和r.raise_for_status来检查
-
原始响应内容
requests获取来自服务器的原始套接字响应
>>> import requests >>> r = requests.get("http://httpbin.org/get", stream=True) >>> r.raw <urllib3.response.HTTPResponse object at 0x000001B93F230518> >>> r.raw.read(300) b‘, \n "Accept-Encoding": "gzip, deflate", \n "Connection": "close", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.18.4"\n }, \n "origin": "110.90.39.155", \n "url": "http://httpbin.org/get"\n}\n‘
要在初始请求中设置stream=True,然后用r.raw,可以使用r.raw.read()对内容进行读取
原文地址:https://www.cnblogs.com/sch01ar/p/8413446.html
时间: 2024-11-09 05:45:16