import asyncio
import functools
from concurrent.futures.thread import ThreadPoolExecutor
from requests_html import HTMLSession
import sys
session = HTMLSession() async def get_response(executor, *, url, loop: asyncio.AbstractEventLoop = None, ):
if not loop:
loop = asyncio.get_running_loop()
request = functools.partial(session.get, url)
return loop.run_in_executor(executor, request) async def bulk_requests(executor, *,
urls,
loop: asyncio.AbstractEventLoop = None, ):
for url in urls:
yield await get_response(executor, url=url, loop=loop) def filter_unsuccesful_requests(responses_and_exceptions):
return filter(
lambda url_and_response: not isinstance(url_and_response[1], Exception),
responses_and_exceptions.items()
) async def main():
executor = ThreadPoolExecutor(10)
urls = [
"https://baidu.com",
"https://cnblogs.com",
"https://163.com",
]
requests = [request async for request in bulk_requests(executor, urls=urls, )]
responses_and_exceptions = dict(zip(urls, await asyncio.gather(*requests, return_exceptions=True)))
responses = {url: resp.html for (url, resp) in filter_unsuccesful_requests(responses_and_exceptions)} for res in responses.items():
print(res[1].xpath("//head//title//text()")[0]) for url in urls:
if url not in responses:
print(f"No successful request could be made to {url}. Reason: {responses_and_exceptions[url]}",
file=sys.stderr) asyncio.run(main())

requests_html使用asyncio的更多相关文章

  1. Python标准模块--asyncio

    1 模块简介 asyncio模块作为一个临时的库,在Python 3.4版本中加入.这意味着,asyncio模块可能做不到向后兼容甚至在后续的Python版本中被删除.根据Python官方文档,asy ...

  2. Asyncio中的Task管理

    #!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio import datetime import time from random ...

  3. 使用Asyncio的Coroutine来实现一个有限状态机

    如图: #!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio import datetime import time from ra ...

  4. 在PYTHON3中,使用Asyncio来管理Event loop

    #!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio import datetime import time def functio ...

  5. Python asyncio库的学习和使用

    因为要找工作,把之前自己搞的爬虫整理一下,没有项目经验真蛋疼,只能做这种水的不行的东西...T  T,希望找工作能有好结果. 之前爬虫使用的是requests+多线程/多进程,后来随着前几天的深入了解 ...

  6. python asyncio笔记

    1.什么是coroutine coroutine,最早我是在lua里面看到的,coroutine最大的好处是可以保存堆栈,让程序得以继续执行,在python里面,一般是利用yield来实现,具体可以看 ...

  7. Tornado (and Twisted) in the age of asyncio》

    Tornado (and Twisted) in the age of asyncio>

  8. 【译】深入理解python3.4中Asyncio库与Node.js的异步IO机制

    转载自http://xidui.github.io/2015/10/29/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3python3-4-Asyncio%E5%BA%93% ...

  9. PYTHON ASYNCIO: FUTURE, TASK AND THE EVENT LOOP

    from :http://masnun.com/2015/11/20/python-asyncio-future-task-and-the-event-loop.html Event Loop On ...

随机推荐

  1. DIP常用资源整理

    Deep Learning(深度学习): ufldl的2个教程(这个没得说,入门绝对的好教程,Ng的,逻辑清晰有练习):一 ufldl的2个教程(这个没得说,入门绝对的好教程,Ng的,逻辑清晰有练习) ...

  2. nexus 绑定负载均衡nginx反向代理后 遇到的https问题。

    1.今天搭建maven私服,下载安装好nexus运行后,通过IP可以直接访问,没有问题,如:http://123.123.123.123:8081 就可以进入主页面.没有任何问题. 2.但是他默认是h ...

  3. SQL脚本优化

    1.创建索引一.要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引   (1)在经常需要进行检索的字段上创建索引,比如要按照表字段username进行检索,那么就应 ...

  4. 浅谈String、StringBuffer与StringBuilder

    浅谈String.StringBuffer与StringBuilder   先详细介绍一下String.StringBuffer与StringBuilder String: 官方对String的说明: ...

  5. Junit5常用注解

    0. IDEA中Maven项目测试类的新建方法 a. 如图在src目录下新建文件夹test b. 鼠标右键test,将该文件设置成test source c. 右键需要新建的测试类,如下图操作,选中T ...

  6. 微信公众号开发(三)—— access_token的管理

    上一篇 微信公众号开发(二)—— 微信公众平台接入 让我们的本地工程顺利的接入到微信公众号系统, 那么接下啦我们介绍一个很重要的感念——acess_token (access_token是公众号的全局 ...

  7. 不重启linuxVMWare虚拟机添加虚拟光驱、硬盘

    方法/步骤1: VMware找到要添加虚拟光驱位置,先右击点击设置,如图所示: 步骤2: 出现此界面之后点击添加: 步骤3: 直接点击下一步,如下图所示: 点击完成,天机硬盘就完毕了. 磁盘添加完成, ...

  8. Linux入门基础

    计算机及运维介绍 如何学习Linux 要想学好任何一门学问,不仅要眼睛看,耳朵听,还要动手记,勤思考,多交流甚至尝试着去教会别人 1.1 运维核心职责 网站数据不能丢 网站7*24小时运行 提升用户体 ...

  9. input子系统驱动

    input子系统驱动 框架分析 核心层 文件为:/drivers/input/input.c: 首先找到入口函数为**static int __init input_init(void)**,在该函数 ...

  10. udp广播,单播,多播

    一.单播 (1)简介 两个节点之间的通信,一个发送者一个接收者 (2)特点 1.服务器及时响应客户机的请求. 2.服务器针对每个客户不通的请求发送不通的数据,容易实现个性化服务. 3.允许在Inter ...