色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Python grequests模塊使用場景及代碼實例

瀏覽:39日期:2022-07-14 15:21:30

使用場景:

1) 爬蟲設置ip代理池時驗證ip是否有效

2)進行壓測時,進行批量請求等等場景

grequests 利用 requests和gevent庫,做了一個簡單封裝,使用起來非常方便。

grequests.map(requests, stream=False, size=None, exception_handler=None, gtimeout=None)

Python grequests模塊使用場景及代碼實例

另外,由于grequests底層使用的是requests,因此它支持

GET,OPTIONS, HEAD, POST, PUT, DELETE 等各種http method

所以以下的任務請求都是支持的

grequests.post(url, json={“name”:“zhangsan”})grequests.delete(url)

代碼如下:

import grequestsurls = [ ’http://www.baidu.com’, ’http://www.qq.com’, ’http://www.163.com’, ’http://www.zhihu.com’, ’http://www.toutiao.com’, ’http://www.douban.com’]rs = (grequests.get(u) for u in urls)print(grequests.map(rs)) # [<Response [200]>, None, <Response [200]>, None, None, <Response [418]>]def exception_handler(request, exception): print('Request failed')reqs = [ grequests.get(’http://httpbin.org/delay/1’, timeout=0.001), grequests.get(’http://fakedomain/’), grequests.get(’http://httpbin.org/status/500’)]print(grequests.map(reqs, exception_handler=exception_handler))

實際操作中,也可以自定義返回的結果

修改grequests源碼文件:

例如:

新增extract_item() 函數合修改map()函數

def extract_item(request): ''' 提取request的內容 :param request: :return: ''' item = dict() item['url'] = request.url item['text'] = request.response.text or '' item['status_code'] = request.response.status_code or 0 return itemdef map(requests, stream=False, size=None, exception_handler=None, gtimeout=None): '''Concurrently converts a list of Requests to Responses. :param requests: a collection of Request objects. :param stream: If True, the content will not be downloaded immediately. :param size: Specifies the number of requests to make at a time. If None, no throttling occurs. :param exception_handler: Callback function, called when exception occured. Params: Request, Exception :param gtimeout: Gevent joinall timeout in seconds. (Note: unrelated to requests timeout) ''' requests = list(requests) pool = Pool(size) if size else None jobs = [send(r, pool, stream=stream) for r in requests] gevent.joinall(jobs, timeout=gtimeout) ret = [] for request in requests: if request.response is not None: ret.append(extract_item(request)) elif exception_handler and hasattr(request, ’exception’): ret.append(exception_handler(request, request.exception)) else: ret.append(None) yield ret

可以直接調用:

import grequestsurls = [ ’http://www.baidu.com’, ’http://www.qq.com’, ’http://www.163.com’, ’http://www.zhihu.com’, ’http://www.toutiao.com’, ’http://www.douban.com’]rs = (grequests.get(u) for u in urls)response_list = grequests.map(rs, gtimeout=10)for response in next(response_list): print(response)

支持事件鉤子

def print_url(r, *args, **kwargs):print(r.url)url = “http://www.baidu.com”res = requests.get(url, hooks={“response”: print_url})tasks = []req = grequests.get(url, callback=print_url)tasks.append(req)ress = grequests.map(tasks)print(ress)

Python grequests模塊使用場景及代碼實例

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 中文精品99久久国产 | 亚洲一片 | 精品一区二区三区在线播放 | 国产成人综合网在线播放 | 国产一级做a爰片久久毛片99 | 国产黄色网 | 国产三级a三级三级午夜 | 国产成人黄网在线免 | 国产成人精品综合在线观看 | 亚洲欧美日产综合在线看 | 成人不卡视频 | 5x性区m免费毛片视频看看 | 三级网站大全 | 国产乱码精品一区二区三区中 | 成人免费看www网址入口 | 超级碰碰碰在线观看 | 欧美一区二区视频三区 | 日韩欧国产精品一区综合无码 | 国产亚洲欧美日韩在线观看一区二区 | 在线观看视频国产 | 久久久久久网址 | 亚洲精品99久久久久中文字幕 | 国产在线观看第一页 | 香蕉三级| 免费成人毛片 | a级毛片毛片免费观看久潮喷 | 国产午夜永久福利视频在线观看 | 国产成年视频 | 久久久久久免费观看 | 99精品视频在线播放2 | 美国毛片亚洲社区在线观看 | 欧美理论大片清免费观看 | 真人真实毛片免费观看 | 亚洲国产日韩欧美综合久久 | 成人a毛片在线看免费全部播放 | 一本三道a无线码一区v小说 | 一级做a爰片久久毛片鸭王 一级做a爰全过程免费视频毛片 | 一级毛片免费播放 | 男人的天堂久久精品激情 | 日韩在线三级 | 真实国产乱人伦在线视频播放 |