在日常开发中,经常会发送各种各样的网络请求。Python中常用的网络请求库有requests
、aiohttp
、httpx
等,httpx
是基于Python3的新一代的网络请求库,它的功能很丰富,做个简答的介绍。
httpx是Python新一代的网络请求库,它包含以下特点
特点
- 基于Python3的功能齐全的http请求模块
- 既能发送同步请求,也能发送异步请求
- 支持HTTP/1.1和HTTP/2
- 能够直接向WSGI应用程序或者ASGI应用程序发送请求
安装
httpx需要Python3.6+(使用异步请求需要Python3.8+)
pip3 install httpx
如果需要使用HTTP/2,则需要安装http2的相关依赖
pip3 install httpx[http2]
简单使用
httpx与requests库的基本使用方法几乎是一模一样的
import httpx
r = httpx.get('https://httpbin.org/get')
print(r)
类似的,我们也可以使用POST, PUT, DELETE, HEAD和OPTIONS等请求方法,如下
r = httpx.post('https://httpbin.org/post', data={'key': 'value'})
r = httpx.put('https://httpbin.org/put', data={'key': 'value'})
r = httpx.delete('https://httpbin.org/delete')
r = httpx.head('https://httpbin.org/get')
r = httpx.options('https://httpbin.org/get')
带有请求头和请求参数的请求
import httpx
headers = {'user-agent': 'my-app/1.0.0'}
params = {'key1': 'value1', 'key2': 'value2'}
url = 'https://httpbin.org/get'
r = httpx.get(url, headers=headers, params=params)
print(r)
print(r.status_code) # 状态码
print(r.encoding) # 文本编码
print(r.text)
print(r.json())
结果如下
<Response [200 OK]>
200
ascii
{
"args": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "my-app/1.0.0",
"X-Amzn-Trace-Id": "Root=1-6139b788-2fd67d5627a5f6de346e154a"
},
"origin": "113.110.227.200",
"url": "https://httpbin.org/get?key1=value1&key2=value2"
}
{'args': {'key1': 'value1', 'key2': 'value2'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'my-app/1.0.0', 'X-Amzn-Trace-Id': 'Root=1-6139b788-2fd67d5627a5f6de346e154a'}, 'origin': '113.110.227.200', 'url': 'https://httpbin.org/get?key1=value1&key2=value2'}
请求带有cookies
import httpx
url = 'http://httpbin.org/cookies'
cookies = {'color': 'green'}
r = httpx.get(url, cookies=cookies)
print(r.json()) # {'cookies': {'color': 'green'}}
设置超时时间
import httpx
r = httpx.get('http://httpbin.org', timeout=0.001)
print(r)
超过设置时间则报httpx.ConnectTimeout: timed out
高级用法
我们使用上面的请求方式时,httpx每次发送请求都需要建立一个新的连接,然而随着请求的数量增加,整个程序的请求效率就会变得很低。
httpx提供了Client
来解决以上问题,Client是基于HTTP
连接池实现的,这意味着当你对一个网站发送多次请求的时候,Client会保持原有的TCP
连接,从而提升程序的执行效率。
使用Client发送请求
创建一个client对象,使用该对象去做相应的请求
import httpx
with httpx.Client() as client:
headers = {'X-Custom': 'value'}
r = client.get('https://example.com', headers=headers)
print(r.text)
跨请求共享配置
我们可以将headers、cookies、params
等参数放在http.Client()
中,在Client下的请求共享这些配置参数
import httpx
headers1 = {'x-auth': 'from-client'}
params1 = {'client_id': '1234'}
url = 'https://example.com'
with httpx.Client(headers=headers1, params=params1) as client:
headers2 = {'x-custom': 'from-request'}
params2 = {'request_id': '4321'}
r1 = client.get(url)
print(r1.request.headers)
r2 = client.get(url, headers=headers2, params=params2)
print(r2.request.headers)
结果如下
Headers({'host': 'example.com', 'accept': '*/*', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.19.0', 'x-auth': 'from-client'})
Headers({'host': 'example.com', 'accept': '*/*', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive', 'user-agent': 'python-httpx/0.19.0', 'x-auth': 'from-client', 'x-custom': 'from-request'})
可以看出,r1的请求头包含{‘x-auth’: ‘from-client’}, r2虽然配置了headers2,但由于里面的headers1和headers2的参数不同,Client会合并这两个headers的参数作为一个新的headers(如果参数相同,则headers2的参数会覆盖headers1的参数)。
HTTP代理
httpx可以通过设置proxies
参数来使用http代理,我们也可以使用不同的代理来分别处理http和https协议的请求,假设有如下两个代理
import httpx
proxies = {
'http://': 'http://localhost:8080', # 代理1
'https://': 'http://localhost:8081', # 代理2
}
url = 'https://example.com'
with httpx.Client(proxies=proxies) as client:
r1 = client.get(url)
print(r1)
上面的代理只是示范,实际场景下请替换成有效的ip代理
还有一点需要注意的是,httpx的代理参数proxies只能在httpx.Client()中添加,client.get()是没有这个参数的。
超时处理
默认情况下,httpx到处都做了严格的超时处理,默认时间为5秒,超过5秒无响应则报TimeoutException
# 普通请求:
httpx.get('http://example.com/api/v1/example', timeout=10.0)
# client实例:
with httpx.Client() as client:
client.get("http://example.com/api/v1/example", timeout=10.0)
SSL验证
当请求https协议的链接时,发出的请求需要验证所请求主机的身份,因此需要SSL证书来取得服务器的信任后。
如果要使用自定义的CA证书,则可以使用verify参数
import httpx
r = httpx.get("https://example.org", verify="path/to/client.pem")
或者你可以完全禁用SSL验证(不推荐)。
import httpx
r = httpx.get("https://example.org", verify=False)
异步支持
默认情况下,httpx使用标准的同步请求方式,如果需要的话,我们也可以使用它提供的异步client来发送相关请求。
使用异步client比使用多线程发送请求更加高效,更能体现明显的性能优势,并且它还支持WebSocket等长网络连接。
异步请求
使用async/await
语句来进行异步操作,创建一个httpx.AsyncClient()
对象
import asyncio
import httpx
async def main():
async with httpx.AsyncClient() as client: # 创建一个异步client
r = await client.get('https://www.example.com/')
print(r)
if __name__ == '__main__':
asyncio.run(main())
同步请求与异步请求的比较
我们来尝试使用同步和异步的方法进行请求,对比两种不同的方法的效率情况。
同步请求
import time
import httpx
def main():
with httpx.Client() as client:
for i in range(300):
res = client.get('https://www.example.com')
print(f'第{i + 1}次请求,status_code = {res.status_code}')
if __name__ == '__main__':
start = time.time()
main()
end = time.time()
print(f'同步发送300次请求,耗时:{end - start}')
同步发送300次请求,耗时:49.65340781211853
异步请求
import asyncio
import time
import httpx
async def req(client, i):
res = await client.get('https://www.example.com')
print(f'第{i + 1}次请求,status_code = {res.status_code}')
return res
async def main():
async with httpx.AsyncClient() as client:
task_list = [] # 任务列表
for i in range(300):
res = req(client, i)
task = asyncio.create_task(res) # 创建任务
task_list.append(task)
await asyncio.gather(*task_list) # 收集任务
if __name__ == '__main__':
start = time.time()
asyncio.run(main())
end = time.time()
print(f'异步发送300次请求,耗时:{end - start}')
异步发送300次请求,耗时:2.5227813720703125 (由于是异步执行的,所以打印的i值是无序的)
从两个例子可以看出,异步请求明显比同步请求的效率高很多。
以上就是httpx库的基本使用方法,想了解更多可以去 httpx官方文档中查看。
实际案例分析:
网站的链接:https://pixabay.com/videos/search/mobile/?pagi=1 打来浏览器,抓个包,看看请求的http请求协议,都是2.0 3.0时代的。
如果此时 我们直接用requests 发起请求,你会直接得到403的状态码
代码如下:
import requests
url = "https://pixabay.com/videos/search/mobile/?pagi=1"
payload={}
headers = {
'authority': 'pixabay.com',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
'cache-control': 'no-cache',
'cookie': 'csrftoken=XjzXsbZaOabJisJUulvsO4GFSSwL4QsAZMOmxX4DS7Dl6nSWKnesn8khkBSQe751; anonymous_user_id=43a54955d85e4b23b60b1a26eb26ca1e; _ga=GA1.2.1182272653.1652781888; _gid=GA1.2.147490439.1652781888; is_human=1; _hjSessionUser_920442=eyJpZCI6IjAwYjczYmZkLTI2NTAtNTA0Yi05Yzk1LTQ2OWI1ZGY5NTk4MCIsImNyZWF0ZWQiOjE2NTI3ODE4ODg4NjgsImV4aXN0aW5nIjp0cnVlfQ==; g_state={"i_p":1652789845926,"i_l":1}; OptanonConsent=isGpcEnabled=0&datestamp=Thu+May+19+2022+10%3A47%3A31+GMT%2B0800+(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&version=6.31.0&isIABGlobal=false&hosts=&consentId=d46d6d33-cc93-4dc6-b632-c4c129a15e52&interactionCount=1&landingPath=NotLandingPage&groups=C0001%3A1%2CC0002%3A1%2CC0003%3A1%2CC0004%3A1&AwaitingReconsent=false; client_width=1402; __cf_bm=RRRlYNCyTlXm2Iuy0u3NMZ2bLLC2YKmOhwO4vP_3Y.k-1653012217-0-AUhbv75h3cTIxlKe8AavfsQQQWic6b8EcoCXTpBCRvPbY61G4Y2g3Hl3oKbGHj8TT6qwPCooiKKcBY2Kg3f8NvM=; _hjIncludedInSessionSample=0; _hjSession_920442=eyJpZCI6ImNmNjczNjFjLTI5ZDEtNGEwMC1iOGMxLWUyMjhlYWNiNjFiNyIsImNyZWF0ZWQiOjE2NTMwMTIyNDg4NTAsImluU2FtcGxlIjpmYWxzZX0=; _hjAbsoluteSessionInProgress=0',
'pragma': 'no-cache',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'none',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
结果如下:
这个时候换成用httpx
发起请求,就能成功 拿到数据。
代码如下:
import httpx
import requests
headers = {
'Host': 'pixabay.com',
'Cookie': 'csrftoken=XjzXsbZaOabJisJUulvsO4GFSSwL4QsAZMOmxX4DS7Dl6nSWKnesn8khkBSQe751; anonymous_user_id=43a54955d85e4b23b60b1a26eb26ca1e; _hjSessionUser_920442=eyJpZCI6IjAwYjczYmZkLTI2NTAtNTA0Yi05Yzk1LTQ2OWI1ZGY5NTk4MCIsImNyZWF0ZWQiOjE2NTI3ODE4ODg4NjgsImV4aXN0aW5nIjp0cnVlfQ==; g_state={"i_p":1652789845926,"i_l":1}; __cf_bm=ngVJxhdm293qZVFL0k4J6P8IR.f8jiezPa8Qqdzl39U-1652923980-0-ATh4o9d6ifrIg4Itgr2vz5k+uVNJ5s2OPaO7MbS8GyjoQ2ySmNs5inOwjC3EuIzZe1WQs21ji5d5c9jlfxN5Y6o=; _hjIncludedInSessionSample=0; _hjSession_920442=eyJpZCI6IjJmZDg5MTE3LWM1Y2UtNGI5OC1hNDdhLTI1MjJhZGU0MGU5YyIsImNyZWF0ZWQiOjE2NTI5MjM5ODI0ODcsImluU2FtcGxlIjpmYWxzZX0=; _hjAbsoluteSessionInProgress=0; client_width=400; __cf_bm=o4KYN07pTtxKgaZIU4wwAEp5eZ4RfuZUsm_Zo7lqjAc-1652929866-0-ARRIIYvWuFNZ2yB8z971fSej9G7IG//ezJoSJXEVMSgUAzIPxhQl6oaWG+q2ICeIDwtypsdBLq88xsULY6LhJ/I=',
'cache-control': 'max-age=0',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"',
'sec-ch-ua-mobile': '?1',
'sec-ch-ua-platform': '"Android"',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Mobile Safari/537.36',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-fetch-site': 'same-origin',
'sec-fetch-mode': 'navigate',
'sec-fetch-user': '?1',
'sec-fetch-dest': 'document',
'referer': 'https://pixabay.com/videos/search/mobile/',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8'
}
cookies = {
"csrftoken": "7SYEIZA24IkrXS4YKBAdYXlkDjquOwuwLm50sWYuf8Tld9xpCcseJlEUjVAwkF94",
"anonymous_user_id": "933827e7dcb142cba0d1f77c74e8030f",
"__cf_bm": "48bls4mmUkRjzkI7whhsAASMyRfx4DsuKmMY3m7qego-1652928880-0-AYXraIuK25/Kbg34fQjutLoWK6dabQSSFGjlFifhg788cy/YjXZbvsAJLhGQaXFC8N7k+u3OcEqiHlw+JOBrSdc=",
"_ga": "GA1.2.2057578463.1652928882",
"_gid": "GA1.2.1896441176.1652928882",
"_gat_UA-20223345-1": "1",
"client_width": "1903",
"_hjSessionUser_920442": "eyJpZCI6Ijg2YTYxNzliLWY2ZGItNTUxOS1iOWYwLWI4ZjZiYjQyY2ZhYiIsImNyZWF0ZWQiOjE2NTI5Mjg4ODM1NTksImV4aXN0aW5nIjpmYWxzZX0=",
"_hjFirstSeen": "1",
"_hjIncludedInSessionSample": "0",
"_hjSession_920442": "eyJpZCI6IjE2YzUyYTVkLTk1ZTItNGQ0OS04NGI0LTMwN2EyZjg0NzQyYSIsImNyZWF0ZWQiOjE2NTI5Mjg4ODM1NjUsImluU2FtcGxlIjpmYWxzZX0=",
"_hjAbsoluteSessionInProgress": "0",
"is_human": "1",
"OptanonConsent": "isGpcEnabled=0&datestamp=Thu+May+19+2022+10%3A55%3A18+GMT%2B0800+(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&version=6.31.0&isIABGlobal=false&hosts=&consentId=785e8522-bf83-429b-acf5-48079173e4de&interactionCount=1&landingPath=https%3A%2F%2Fpixabay.com%2Fvideos%2Fsearch%2Fmobile%2F%3Fpagi%3D1&groups=C0001%3A1%2CC0002%3A0%2CC0003%3A0%2CC0004%3A0"
}
url = "https://pixabay.com/videos/search/mobile/?pagi=1"
with httpx.Client(http2=True) as client:
response = client.get(url, headers=headers, cookies=cookies)
print(response.status_code)
print(response.text)
结果:
文章转载于:https://blog.51cto.com/u_15127674/3872190