• Python并发请求之requests_future模块使用


    # -*- coding: utf-8 -*-
    # @Time : 2019-12-09 10:00
    # @Author : cxa
    # @File : demo.py
    # @Software: PyCharm
    from requests_futures.sessions import FuturesSession
    from concurrent.futures import as_completed
    from lxml import html
    import time
    
    url = ["http://www.baidu.com", "http://www.163.com", "http://www.google.com", "http://www.cnblogs.com/c-x-a"]
    
    
    def get_node(source, x=".//head/title//text()"):
        root = html.fromstring(source)
        node = root.xpath(x)
        return node
    
    
    def response_hook(resp, *args, **kwargs):
        start = time.time()
        resp.encoding = resp.apparent_encoding
        resp.data = resp.text
        resp.code = resp.status_code
        resp.headers = resp.headers
        resp.elapsed = time.time() - start
    
    
    def get_req():
        with FuturesSession(max_workers=4) as session:
            futures = [session.get(i, hooks={"response": response_hook}) for i in url]
            for future in as_completed(futures):
                resp = future.result()
                print("状态码", resp.code)
                print("标题", get_node(resp.data)[0])
                print("耗时", resp.elapsed)
                print("="*30)
    
    if __name__ == '__main__':
        get_req()
    
    

    修改之后

    from requests_futures.sessions import FuturesSession
    from concurrent.futures import as_completed
    from lxml import html
    import time
    
    url = ["http://www.baidu.com", "http://www.163.com", "http://www.google.com", "http://www.cnblogs.com/c-x-a"]
    
    
    class MySession(FuturesSession):
    
        def request(self, method, url, hooks=None, *args, **kwargs):
            start = time.time()
            if hooks is None:
                hooks = {}
    
            def response_hook(resp, *args, **kwargs):
                resp.encoding = resp.apparent_encoding
                resp.data = resp.text
                resp.code = resp.status_code
                resp.headers = resp.headers
                resp.elapsed = time.time() - start
    
            try:
                if isinstance(hooks['response'], (list, tuple)):
                    hooks['response'].insert(0, response_hook)
                else:
                    hooks['response'] = [response_hook, hooks['response']]
            except KeyError:
                hooks['response'] = response_hook
    
            return super(MySession, self).request(method, url, hooks=hooks, *args, **kwargs)
    
    
    def get_node(source, x=".//head/title//text()"):
        root = html.fromstring(source)
        node = root.xpath(x)
        return node
    
    
    def get_req():
        with MySession(max_workers=4) as session:
            futures = [session.get(i) for i in url]
            for future in as_completed(futures):
                resp = future.result()
                print("状态码", resp.code)
                print("标题", get_node(resp.data)[0])
                print("耗时", resp.elapsed)
                print("=" * 30)
    
    
    if __name__ == '__main__':
        get_req()
    
  • 相关阅读:
    react跳转url,跳转外链,新页面打开页面
    如何把大段文字转为带html标签的文字
    react给一个div行内加背景图片并实现cover覆盖模式居中显示
    react获取当前页面的url参数
    mongodb查询数据库中某个字段中的值包含某个字符串的方法
    react实现多行文本超出加省略号
    小程序首页获取数据给数组赋值,实现加载更多,以及遇到的坑
    小程序渲染html的两种方法
    小程序url传参如何写变量
    小程序在父组件执行子组件方法,可适用于下拉刷新上拉加载之后执行子组件方法
  • 原文地址:https://www.cnblogs.com/c-x-a/p/12009565.html
Copyright © 2020-2023  润新知