一个基于gevent的异步请求库 - grequests
1.安装
1 | pip install grequests - i http: / / mirrors.aliyun.com / pypi / simple / - - trusted - host mirrors.aliyun.com |
2.基础用法教程
用grequests.map()方法时,传入的必须是生成器或列表,下面是用小括号创建的是生成器,用方括号也行,生成列表。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import grequests import time urls = [ "http://www.baidu.com" , "http://www.taobao.com" , "http://www.xinwen.com" , "http://www.meituan.com" , "http://www.jingdong.com" , ] reqs = (grequests.get(u) for u in urls) start_time = time.time() responses = grequests. map (reqs) end_time = time.time() print (f "耗时:{end_time - start_time}秒" ) |
3.异常处理
异步请求处理异常跟普通requests不太一样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import grequests import time urls = [ "http://www.baidu.com" , "http://www.taobao.com" , "http://www.xinwen.com" , "http://www.meituan.com" , "http://www.jingdong.com" , ] reqs = (grequests.get(u) for u in urls) start_time = time.time() responses = grequests. map (reqs) end_time = time.time() print (f "耗时:{end_time - start_time}秒" ) def handle_error(request, exception): print (f "哎呀,出错了:{request.url}" ) return None # 带异常处理的请求 res = grequests. map (reqs, exception_handler = handle_error) |
4. 并发数控制
1 2 | # size参数控制并发数,这里限制最多同时发5个请求 responses = grequests. map (reqs, size = 5 ) |
5. 发送不同的请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | post_urls = [ 'http://httpbin.org/post' ] * 3 post_reqs = [grequests.post(u, json = { 'msg' : 'hello' }) for u in post_urls] # 各种不同的请求 mixed_reqs = [ # 发请求时加上超时设置,避免某个请求卡住影响整体效率 grequests.get( 'http://httpbin.org/get' , timeout = 5 ), grequests.post( 'http://httpbin.org/post' , data = { 'key' : 'value' }, timeout = 5 ), grequests.put( 'http://httpbin.org/put' , json = { 'name' : 'cat' }, timeout = 5 ), grequests.patch( 'http://httpbin.org/put' , json = { 'name' : 'cat' }, timeout = 5 ), grequests.delete( 'http://httpbin.org/put' , json = { 'name' : 'cat' }, timeout = 5 ) ] responses = grequests. map (mixed_reqs) |
1 2 3 4 5 6 7 8 9 | # 1. 带请求头 headers = { 'User-Agent' : 'Mozilla/5.0 ...' } reqs = (grequests.get(u, headers = headers) for u in urls) # 2. 使用代理 proxies = { 'http' : 'http://10.10.10.1:8888' } reqs = (grequests.get(u, proxies = proxies) for u in urls) # 3. 带cookies cookies = { 'session' : 'abc123' } reqs = (grequests.get(u, cookies = cookies) for u in urls) |
grequests的速度确实很快,但用的时候也要注意几点:
1. 不是所有网站都能抗住并发请求,该加延迟的地方得加延迟;
2. 代码里最好加上异常处理,不然一个请求出错可能影响整体;数据量特别大的时候,建议分批请求,避免内存爆掉。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix