亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python grequests模塊使用場(chǎng)景及代碼實(shí)例

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

使用場(chǎng)景:

1) 爬蟲(chóng)設(shè)置ip代理池時(shí)驗(yàn)證ip是否有效

2)進(jìn)行壓測(cè)時(shí),進(jìn)行批量請(qǐng)求等等場(chǎng)景

grequests 利用 requests和gevent庫(kù),做了一個(gè)簡(jiǎn)單封裝,使用起來(lái)非常方便。

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

Python grequests模塊使用場(chǎng)景及代碼實(shí)例

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

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

所以以下的任務(wù)請(qǐng)求都是支持的

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))

實(shí)際操作中,也可以自定義返回的結(jié)果

修改grequests源碼文件:

例如:

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

def extract_item(request): ''' 提取request的內(nèi)容 :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

可以直接調(diào)用:

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模塊使用場(chǎng)景及代碼實(shí)例

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 高清一区二区三区视频 | 开心成人激情 | 欧美特欧美特级一片 | 久久99热精品免费观看无卡顿 | 亚洲黄色一级毛片 | xvideos亚洲入口地址 | 国产精品亚洲一区二区三区久久 | 九一视频在线免费观看 | 美国一级大黄一片免费的网站 | 一级黄a| 国产午夜免费视频片夜色 | 曰曰啪天天拍视频在线 | 国产成人99久久亚洲综合精品 | 国产精品馆 | 成人三级影院 | 超级碰碰碰免费视频播放 | 中文字幕国产欧美 | 国产第一区精品视频ai换脸 | 色国产在线视频一区 | 国产精品亚洲第一区广西莫菁 | 国产一区日韩二区欧美三区 | 美国人妖欧美性xxxxk妖 | 久久久久久免费一区二区三区 | 精品外国呦系列在线观看 | 国产极品视频 | 亚洲黄色网络 | 国产成人啪精品 | 国产一级一片免费播放刺激 | 美女喷水视频在线观看 | 国产亚洲欧美在线 | 国产高清在线精品免费不卡 | 韩国一级毛片a级免观看 | 另类免费视频 | 欧美日韩国产另类一区二区三区 | 96精品视频| 欧美黑人巨大肥婆性视频 | 亚洲三级网| 黄色片中国| 2021最新国产成人精品免费 | 日韩毛片 | a一级片 |