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爬虫的模板,类的形式的更多相关文章

  1. C++模板类的使用

    1.定义模板类 通过类似于下面的语法可以定义一个模板类: template<typename T> class Job : public virtual RefBase { public: ...

  2. C++:类模板与模板类

    6.3 类模板和模板类 所谓类模板,实际上是建立一个通用类,其数据成员.成员函数的返回值类型和形参类型不具体指定,用一个虚拟的类型来代表.使用类模板定义对象时,系统会实参的类型来取代类模板中虚拟类型从 ...

  3. C++ 模板类解析

    具体模板类作用这边就不细说了,下面主要是描述下模板类的使用方法以及注意的一些东西. #include <iostream> using namespace std; template &l ...

  4. 使用模板类导致error LNK2019: 无法解析的外部符号

    原地址 1.定义模板类: template<class T> class Stack {....}; 2.定义模板成员函数: 每个函数头都要以相同的模板声明打头,并将类限定符改成:类名&l ...

  5. 开涛spring3(7.2) - 对JDBC的支持 之 7.2 JDBC模板类

    7.2  JDBC模板类 7.2.1  概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDB ...

  6. 7.2 C++模板类实例化

    参考:http://www.weixueyuan.net/view/6399.html 总结: array < int >表明用int类型来代替模板类中的类参数“T”,编译器会将模板类ar ...

  7. [C++]模板类和模板函数

    参考: C++ 中模板使用详解 C++模板详解 概念 为了避免因重载函数定义不全面而带来的调用错误,引入了模板机制 定义 模板是C++支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模 ...

  8. (转)JDBC模板类。

    Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDBC模板类是第一种工作模式. JdbcTempl ...

  9. spring3:对JDBC的支持 之 JDBC模板类

    7.2  JDBC模板类 7.2.1  概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTemplate是core包的核心类,所以其他模板类都是基于它封装完成的,JDB ...

随机推荐

  1. Stop-VM

    stop-vm vm01 -force  正常关机,留给Guest 5分钟保存数据,然后关闭 stop-vm vm02 -turnoff 断电关机 Windows Server 2008 R2默认没有 ...

  2. Java实例---简单的数据库操作

    源码分析 DAOFactory.java package cn.ftl.mysql ; public class DAOFactory { public static IEmpDAO getIEmpD ...

  3. C#/Entity Frame Core 使用Linq 进行分页 .Skip() .Take() 的使用方法

    一般使用格式为 int pagesize = 分页大小(每一页大小)int pageindex = 第几页(一般这个变量是随循环递增的) 使用方法.Skip(pagesize*pageindex).T ...

  4. 给腾讯云Linux主机创建Swap文件

    新买的腾讯云主机没有提供Swap分区 理由是由于主机经常因为内存使用率过高,频繁使用Swap,导致磁盘IO过高,服务器整体性能反而下降. 不过用户依然可以使用Swap文件的方式添加Swap. 方法如下 ...

  5. December 14th 2016 Week 51st Wednesday

    Everything has its time and that time must be watched. 万物皆有时,时来不可失. Everything has its time, and I r ...

  6. 【PHP】 mysqli_autocommit() 函数

    //获取每一篇文章的内容 function getPost($f_parent_id, $f_title, $f_username, $f_board_id,$f_post_time, $f_ip,$ ...

  7. 【webpack】理解配置文件

    学习链接: http://blog.csdn.net/hongchh/article/details/55113751 https://segmentfault.com/a/1190000009356 ...

  8. 27、springboot整合RabbitMQ(1)

    RabbitMQ整合 使用dockers下载带management的版本,该版本是带web界面的,可操作性比较强

  9. PHP异步:在PHP中使用 fsockopen curl 实现类似异步处理的功能

    PHP从主流来看,是一门面向过程的语言,它的最大缺点就是无法实现多线程管理,其程序的执行都是从头到尾,按照逻辑一路执行下来,不可能出现分支,这一点是限制php在主流程序语言中往更高级的语言发展的原因之 ...

  10. Kali-linux免杀Payload生成工具Veil

    Veil是一款利用Metasploit框架生成相兼容的Payload工具,并且在大多数网络环境中能绕过常见的杀毒软件.本节将介绍Veil工具的安装及使用. 在Kali Linux中,默认没有安装Vei ...