使用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 ...
随机推荐
- 8.第七篇 验证kube-apiserver及kubeconfig配置详解
文章转载自:https://mp.weixin.qq.com/s?__biz=MzI1MDgwNzQ1MQ==&mid=2247483818&idx=1&sn=7572b791 ...
- Elastic:创建你的第一个Elastic alert
文章转载自:https://blog.csdn.net/UbuntuTouch/article/details/105340379 在Elasticsearch可以提供给我们数据的存储及快速的搜索,但 ...
- Logstash:在 Docker 中部署 Logstash
文章转载自:https://elasticstack.blog.csdn.net/article/details/116516923 创建一个目录 docker-logstash.在该目录下,有如下的 ...
- elk使用微信ElartAlert企业微信告警,自定义告警内容
第一种方式 alert: - "elastalert_modules.wechat_qiye_alert.WeChatAlerter" alert_text: " === ...
- Docker搭建kafka及监控
环境安装 docker安装 yum update yum install docker # 启动 systemctl start docker # 加入开机启动 systemctl enable do ...
- Python 实现Tracert追踪TTL值
Tracert 命令跟踪路由原理是IP路由每经过一个路由节点TTL值会减一,假设TTL值=0时数据包还没有到达目标主机,那么该路由则会回复给目标主机一个数据包不可达,由此我们就可以获取到目标主机的IP ...
- P3998 [SHOI2013]发微博 方法记录
原题链接 [SHOI2013]发微博 题目描述 刚开通的 SH 微博共有 \(n\) 个用户(\(1\sim n\) 标号),在这短短一个月的时间内,用户们活动频繁,共有 \(m\) 条按时间顺序的记 ...
- Spring的自动装配和注解
Bean的自动装配 自动装配说明 自动装配是使用spring满足bean依赖的一种方法 spring会在应用上下文中为某个bean寻找其依赖的bean. Spring的自动装配需要从两个角度来实现,或 ...
- 创建第一个springboot项目、用springboot实现页面跳转、@Controller和@RestController的区别
文章目录 一.第一个spring boot项目 二.spring boot跳转到指定页面 三.怎样将后台的信息传递到前台 四. @Controller和@RestController的区别? 一.第一 ...
- 齐博x1where 标签动态变量查询/where 实现条件筛选与数据关联
大家可能对union参数不太习惯的话,也可以用where语句加入动态变量查询,比如 where="fid=$fid&uid=$info[uid]&pid>=$info. ...