使用sanic框架实现分布式爬虫
bee_server.py
from sanic import Sanic
from sanic import response
from urlpool import UrlPool #初始化urlpool,很久需要进行修改
urlpool = UrlPool(__file__) #初始化url
urlpool.add('https://news.sina.com.cn/')
app = Sanic(__name__) @app.listener("after_server_stop")
async def cache_urlpool(app,loop):
global urlpool
print("caching urlpool after_server_stop")
del urlpool
print("bye!") @app.route("/task")
async def task_get(request):
count = request.args.get("count",10)
try:
count = int(count)
except:
count = 10
urls = urlpool.pop(count)
return response.json(urls) @app.route("/task",methods=["POST",])
async def task_post(requrst):
result = requrst.json()
urlpool.set_status(result['url'],result['status'])
if result['url_real'] != result['url']:
urlpool.set_status(result["url_real"],result["status"])
if result["newurls"]:
print("receive URLs:",len(result["newurls"]))
for url in result["newurls"]:
urlpool.add(url)
return response.text("ok") if __name__ == '__main__':
app.run(
host='127.0.0.1',
port = 8080,
debug=False,
access_log=False,
workers=1
)
bee_client.py
import aiohttp
import json
import asyncio
import traceback
import time class CrawlerClient:
def __init__(self):
self._workers = 0
self.workers_max = 10
self.server_host = "localhost"
self.server_port = 8080
self.session = aiohttp.ClientSession(loop=self.loop)
self.queue = asyncio.Queue async def get_url(self):
count = self.workers_max-self.queue.qsize()
if count <= 0:
print("no need to get urls this time") url = "http://%S:%S/task?count=%s" % (self.server_host,self.server_port,count)
try:
async with self.session.get(url,timeout = 3) as response:
if response.status not in [200,201]:
return
jsn = await response.text()
urls = json.loads(jsn)
msg = ('get_urls() to get [%s] but got[%s],@%s') % (count,len(urls),time.strftime('%Y-%m-%d %H:%M:%S'))
print(msg)
for lv in urls.items():
await self.queue.put(lv)
print()
except:
traceback.print_exc()
return async def send_result(self,result):
'''
result = {
"url“:url,
'url_real':response.url,
'status':status,
"newurls":newurls,
}
'''
url = "http://%S:%S/task" % (self.server_host,self.server_port)
try:
async with self.session.post(url,json = result,timeout = 3) as response:
response.status
except:
traceback.print_exc()
pass
使用sanic框架实现分布式爬虫的更多相关文章
- 基于Python使用scrapy-redis框架实现分布式爬虫
1.首先介绍一下:scrapy-redis框架 scrapy-redis:一个三方的基于redis的分布式爬虫框架,配合scrapy使用,让爬虫具有了分布式爬取的功能.github地址: https: ...
- 基于scrapy框架的分布式爬虫
分布式 概念:可以使用多台电脑组件一个分布式机群,让其执行同一组程序,对同一组网络资源进行联合爬取. 原生的scrapy是无法实现分布式 调度器无法被共享 管道无法被共享 基于 scrapy+redi ...
- Cola:一个分布式爬虫框架 - 系统架构 - Python4cn(news, jobs)
Cola:一个分布式爬虫框架 - 系统架构 - Python4cn(news, jobs) Cola:一个分布式爬虫框架 发布时间:2013-06-17 14:58:27, 关注:+2034, 赞美: ...
- 基于Python,scrapy,redis的分布式爬虫实现框架
原文 http://www.xgezhang.com/python_scrapy_redis_crawler.html 爬虫技术,无论是在学术领域,还是在工程领域,都扮演者非常重要的角色.相比于其他 ...
- 分布式爬虫框架XXL-CRAWLER
<分布式爬虫框架XXL-CRAWLER> 一.简介 1.1 概述 XXL-CRAWLER 是一个分布式爬虫框架.一行代码开发一个分布式爬虫,拥有"多线程.异步.IP动态代理.分布 ...
- Scrapy+Scrapy-redis+Scrapyd+Gerapy 分布式爬虫框架整合
简介:给正在学习的小伙伴们分享一下自己的感悟,如有理解不正确的地方,望指出,感谢~ 首先介绍一下这个标题吧~ 1. Scrapy:是一个基于Twisted的异步IO框架,有了这个框架,我们就不需要等待 ...
- 爬虫开发14.scrapy框架之分布式操作
分布式爬虫 一.redis简单回顾 1.启动redis: mac/linux: redis-server redis.conf windows: redis-server.exe redis-wi ...
- 基于redis的简易分布式爬虫框架
代码地址如下:http://www.demodashi.com/demo/13338.html 开发环境 Python 3.6 Requests Redis 3.2.100 Pycharm(非必需,但 ...
- Scrapy框架之基于RedisSpider实现的分布式爬虫
需求:爬取的是基于文字的网易新闻数据(国内.国际.军事.航空). 基于Scrapy框架代码实现数据爬取后,再将当前项目修改为基于RedisSpider的分布式爬虫形式. 一.基于Scrapy框架数据爬 ...
- Python分布式爬虫必学框架Scrapy打造搜索引擎
Python分布式爬虫必学框架Scrapy打造搜索引擎 部分课程截图: 点击链接或搜索QQ号直接加群获取其它资料: 链接:https://pan.baidu.com/s/1-wHr4dTAxfd51M ...
随机推荐
- Prometheus使用nginx 设置二级路径反向代理
1.nginx 设置 location /promethues/ { proxy_pass http://10.xx.xxx.55:9090/prometheus/; } 2.设置prometheus ...
- Beats:在 Beats 中实现动态 pipeline
转载自:https://blog.csdn.net/UbuntuTouch/article/details/107127197 在我们今天的练习中,我们将使用 Metricbeat 来同时监控 kib ...
- 企业运维 | MySQL关系型数据库在Docker与Kubernetes容器环境中快速搭建部署主从实践
[点击 关注「 WeiyiGeek」公众号 ] 设为「️ 星标」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 本章目录 目录 ...
- C++ 栈和典型迷宫问题
C++ 栈和迷宫问题 1. 前言 栈是一种受限的数据结构,要求在存储数据时遵循先进后出(Last In First Out)的原则.可以把栈看成只有一个口子的桶子,进和出都是走的这个口子(也称为栈顶) ...
- Java注解(2):实现自己的ORM
搞过Java的码农都知道,在J2EE开发中一个(确切地说,应该是一类)很重要的框架,那就是ORM(Object Relational Mapping,对象关系映射).它把Java中的类和数据库中的表关 ...
- echarts的使用 超好用的报表制作、数据的图形化展示
地址链接:https://echarts.apache.org/zh/index.html 1.图形选择 2.对应的js代码
- JavaScript基础&实战(5)js中的数组、forEach遍历、Date对象、Math、String对象
文章目录 1.工厂方法创建对象 1.1 代码块 1.2.测试结果 2.原型对象 2.1 代码 2.2 测试结果 3.toString 3.1 代码 3.2 测试结果 4.数组 4.1 代码 5.字面量 ...
- .net lambda表达式合并
事情的起因是公司一个小伙子问了我个问题 "海哥,来帮我看下这段代码怎么不行" Func<Report,bool> nameFilter = x=>x.Name = ...
- 齐博x1模型里边钩子的创建与使用
在模型里边的钩子创建与使用方法跟在控制器里边的钩子创建及使用方法是有所区别的在模型里边创建的钩子,你可以理解为执行一个函数,是无法调用模型里边的类的方法及属性的.比如系统文件\application\ ...
- day51-正则表达式02
正则表达式02 5.4正则表达式语法02 5.4.6捕获分组 详见5.3.3 例子 package li.regexp; import java.util.regex.Matcher; import ...