requests_html使用asyncio
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的更多相关文章
- Python标准模块--asyncio
1 模块简介 asyncio模块作为一个临时的库,在Python 3.4版本中加入.这意味着,asyncio模块可能做不到向后兼容甚至在后续的Python版本中被删除.根据Python官方文档,asy ...
- Asyncio中的Task管理
#!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio import datetime import time from random ...
- 使用Asyncio的Coroutine来实现一个有限状态机
如图: #!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio import datetime import time from ra ...
- 在PYTHON3中,使用Asyncio来管理Event loop
#!/usr/bin/env python # -*- coding: utf-8 -*- import asyncio import datetime import time def functio ...
- Python asyncio库的学习和使用
因为要找工作,把之前自己搞的爬虫整理一下,没有项目经验真蛋疼,只能做这种水的不行的东西...T T,希望找工作能有好结果. 之前爬虫使用的是requests+多线程/多进程,后来随着前几天的深入了解 ...
- python asyncio笔记
1.什么是coroutine coroutine,最早我是在lua里面看到的,coroutine最大的好处是可以保存堆栈,让程序得以继续执行,在python里面,一般是利用yield来实现,具体可以看 ...
- Tornado (and Twisted) in the age of asyncio》
Tornado (and Twisted) in the age of asyncio>
- 【译】深入理解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% ...
- 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 ...
随机推荐
- NetScaler Logs Collection Guide
NetScaler Logs Collection Guide 来源 https://support.citrix.com/article/CTX227560 Article | Authentic ...
- VUE实现微博发布效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 使用百度echarts仿雪球分时图(二)
上一章简单的介绍了一下分时图的构成,其实就是折线图跟柱状图的组成.本来这章打算是把分时图做完,然后再写一章来进行美化和总结,但是仔细观察了一下,发现其实东西还是有点多的.分时图的图表做完后,还要去美化 ...
- 操作xml文件
http://www.cnblogs.com/ 一.xml文件体系如下: <?xml version="1.0" encoding="utf-8" ?&g ...
- VUE【二、选项和生命周期】
vue对象,类似于一个viewModel,是处理页面显示的数据模型的对象 其中会有很多选项,以下为较常用的: 选项 1.data-数据 vue实例会代理其data对象里的所有属性 2.methods- ...
- JavaSpring【四、Bean管理注解实现】
前面讲的Bean相关配置全部是使用xml配置文件或实现接口来实现的,接下来将比较常用的用法,使用注解实现bean的注入和管理 内容包括 ClassPath扫描与组件管理 类的自动检测与注册bean c ...
- PXE批量部署安装Linux系统
PXE介绍 1)Preboot Excution Environment 预启动执行环境 2)Intel公司研发 3)基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映 ...
- 2.Bacula Server端安装配置
1. Bacula Server端安装配置 1.1. Bacula Server端安装 1.1.1. 安装bacula依赖包 For Centos6: yum install -y mysql ...
- shell 脚本检测端口状态
方法一: # cat check_port.sh #!/bin/bash cat ip.txt|while read line do /usr/bin/nc -w 1 -z $line > /d ...
- 用VNC远程无线站
用VNC远程无线站 第一步: 远程一台我们可以vnc连接的linux电脑(IP1为实际IP地址) vim /etc/ssh/sshd_config 修改:69行为 X11Forwarding no 为 ...