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 ...
随机推荐
- async/await 的引用
static async void Start() { string s = "ass"; Console.WriteLine(getMemory(s)+"Hello W ...
- 怎么解决64位Access与32位不能同时安装的问题
如何在同时安装32位和64位Micsoft Access数据库引擎 由于某些64位应用程序需要访问Access数据库,而访问数据库须使用AccessDataEngine即Access数据库引擎64 ...
- JS 实现继承的方法 ES6 and ES5
继承 ES6 方法 (类的继承) ES6中有一个属性的 extends 语法: • class Father {} • class Son extends Father{} 注意:是子类 ...
- Js-带进度条的轮播图
带进度条的轮播图--原生JS实现 实现了图片自动轮播,左右按钮实现图片左右转换,下方原点或者缩小图点击选择其中的某一张图片,然后有红条实现图片的进度. <div class="cont ...
- json树迭代
getArray(data){ for (var i in data) { if(data[i].disabled){ data[i].disabled = false } if(data[i].ch ...
- C8051F环境搭建
https://www.silabs.com/ USB调试器 U-EC6: 支持JTAG模式.C2模式 JTAG接口定义: 适用型号C8051F00x C8051F01x C8051F02x C805 ...
- 使用nodejs实现OData的batch操作在Marketing Cloud里读取contact信息
我们先来看看Marketing Cloud系统里的contact信息: 一共1218374条数据. 我们用如下的nodejs代码通过OData来获取这些数据: var request = requir ...
- CentOS自动备份MySql
1.确认Crontab是否安装 service crond startcrontab -l 2.编写备份脚本 cd mkdir backup cd backup vim auto.sh /usr/bi ...
- 4.AOP原理模拟
AOP Aspect-Oriented-Programming 面向切面编程 a)是对面向对象的思维方式的有力补充 好处:可以动态的添加和删除在切面上的逻辑而不影响原来的执行代码 a)Fil ...
- dedecms Fatal error: Out of memory
max_execution_time = 300 php执行持续最长时间 比如:网站全站更新max_input_time = 30 php传送数据最长时间 比如:上传大文件memory_l ...