aiohttp爬虫的模板,类的形式
- import asyncio
- import aiohttp
- import async_timeout
- from lxml import html
- from timeit import default_timer as timer
- from db import DBData
- class Crawler:
- def __init__(self, **kwargs):
- self.domains = kwargs["domains"]
- self.max_depth = kwargs["max_depth"]
- self.max_retries = 3
- self.max_workers = 10
- self.Q = asyncio.Queue()
- self.db_Q = asyncio.Queue()
- self.cache = set()
- self.count = 0
- self.loop = asyncio.get_event_loop()
- self.db_data = DBData()
- # Clear
- self.db_data.clear_crawler()
- async def get(self, url, timeout):
- with async_timeout.timeout(timeout):
- async with self.session.get(url) as response:
- return await response.text()
- async def extract_urls(self, url, timeout=10):
- tree = html.fromstring(await self.get(url, timeout))
- # Search only in domains
- return {p for p in tree.xpath("//a/@href")}
- # if any(domain in p for domain in self.domains)}
- async def worker(self):
- while True:
- url, depth, retries = await self.Q.get()
- if url in self.cache:
- self.db_Q.put_nowait(url)
- self.Q.task_done()
- continue
- try:
- new_urls = await self.extract_urls(url)
- except Exception as e:
- if retries <= self.max_retries:
- self.Q.put_nowait((url, depth, retries + 1))
- else:
- print("Error in %s: %s" % (url, repr(e)))
- else:
- self.cache.add(url)
- self.count += 1
- self.db_Q.put_nowait(url)
- print("Depth: %s Retry: %s Visited: %s" % (depth, retries, url))
- if depth+1 <= self.max_depth:
- for x in new_urls:
- self.Q.put_nowait((x, depth + 1, retries))
- self.Q.task_done()
- async def run(self):
- async with aiohttp.ClientSession(loop=self.loop) as session:
- self.session = session
- workers = [self.worker() for _ in range(self.max_workers)]
- workers += [self.write_to_db() for _ in range(self.max_workers)]
- tasks = [self.loop.create_task(x) for x in workers]
- await asyncio.sleep(5)
- await self.Q.join()
- await self.db_Q.join()
- for task in tasks:
- task.cancel()
- def start(self):
- for domain in self.domains:
- print("Crawling %s start..." % domain)
- self.Q.put_nowait((domain, 0, 0))
- start_time = timer()
- self.loop.run_until_complete(asyncio.gather(self.run()))
- self.loop.close()
- runtime = timer() - start_time
- print("Crawling %s end. Exec time: %s. Requests: %s" % (
- domain, runtime, self.count))
- async def write_to_db(self):
- while True:
- address = await self.db_Q.get()
- if await self.db_data.check_url(address) is None:
- self.db_data.add_url(address)
- print("Write to DB: %s" % address)
- self.db_Q.task_done()
- if __name__ == "__main__":
- options = {
- "domains": ["https://www.yahoo.com/news/"],
- "max_depth": 1
- }
- c = Crawler(**options)
- c.start()
aiohttp爬虫的模板,类的形式的更多相关文章
- C++模板类的使用
1.定义模板类 通过类似于下面的语法可以定义一个模板类: template<typename T> class Job : public virtual RefBase { public: ...
- C++:类模板与模板类
6.3 类模板和模板类 所谓类模板,实际上是建立一个通用类,其数据成员.成员函数的返回值类型和形参类型不具体指定,用一个虚拟的类型来代表.使用类模板定义对象时,系统会实参的类型来取代类模板中虚拟类型从 ...
- C++ 模板类解析
具体模板类作用这边就不细说了,下面主要是描述下模板类的使用方法以及注意的一些东西. #include <iostream> using namespace std; template &l ...
- 使用模板类导致error LNK2019: 无法解析的外部符号
原地址 1.定义模板类: template<class T> class Stack {....}; 2.定义模板成员函数: 每个函数头都要以相同的模板声明打头,并将类限定符改成:类名&l ...
- 开涛spring3(7.2) - 对JDBC的支持 之 7.2 JDBC模板类
7.2 JDBC模板类 7.2.1 概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDB ...
- 7.2 C++模板类实例化
参考:http://www.weixueyuan.net/view/6399.html 总结: array < int >表明用int类型来代替模板类中的类参数“T”,编译器会将模板类ar ...
- [C++]模板类和模板函数
参考: C++ 中模板使用详解 C++模板详解 概念 为了避免因重载函数定义不全面而带来的调用错误,引入了模板机制 定义 模板是C++支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模 ...
- (转)JDBC模板类。
Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDBC模板类是第一种工作模式. JdbcTempl ...
- spring3:对JDBC的支持 之 JDBC模板类
7.2 JDBC模板类 7.2.1 概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDB ...
随机推荐
- 配置Sharepoint之后。外网无法访问的问题
Sharepoint配置完成了,projectserver也已经配置完毕.突然遇到一个情况就是外网访问不了,这可麻烦了,费了半天事访问不了等于0啊.没办法,研究吧.在群里问了大神,终于解决了.现将解决 ...
- Windows Server、 Windows 区别
今天脑补了普通Windows 操作系统与Windows Server区别,感觉清楚了很多. Microsoft WindowsServer,是美国微软公司研制的一套操作体系,它面世于1985年,起先仅 ...
- BZOJ 1059 矩阵游戏 二分图匹配
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1059 题目大意: 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏 ...
- Gluon 参数读取
ndarray: save , load from mxnet import nd from mxnet.gluon import nn x = nd.ones(3) # nd.save('x',x) ...
- Pymyaql操作数据库
Pymyaql操作数据库 Python3中专门用于操作Mysql数据库的模块. 一. 导入模块 import pymysql 二. 创建连接 conn=pymysql.connect(host=’12 ...
- bootstrap清除浮动问题
所有的col-样式都是左浮动 <div class="row"> <div class="col-xs-6 col-sm-3">d ...
- Appium基础篇(一)——启动emulator
1. Appium API文档:链接参考 http://appium.io/slate/cn/v/?ruby#appium-介绍. 2. Appium 安装篇:http://www.cnblogs.c ...
- vue-scroll 底部无数据时,底部出现大片的空白
vue-scroll放在vue的项目中,实现下拉刷新的效果,但是发现,不能上拉的bug,上拉了之后,底部出现了一大段的空白,参照GitHub的问题,算是暂时解决了. 不能上拉的原因是:滑动标签里边的内 ...
- Springboot中使用ibatis输出日志
logging.level.org.apache.ibatis=DEBUG logging.level.org.mybatis=DEBUG logging.level.java.sql.Connect ...
- php多进程编程实现与优化
PHP多进程API 创建子进程 @params void @returns int int pcntl_fork(void) 成功时,在父进程执行线程内返回产生的子进程PID,在子进程执行线程内返回0 ...