python使用rabbitmq实现网络爬虫示例
发布时间:2022-04-20 20:11:58
标签:python,rabbitmq,网络爬虫
编写tasks.py
from celery import Celery
from tornado.httpclient import HTTPClient
app = Celery('tasks')
app.config_from_object('celeryconfig')
@app.task
def get_html(url):
http_client = HTTPClient()
try:
response = http_client.fetch(url,follow_redirects=True)
return response.body
except httpclient.HTTPError as e:
return None
http_client.close()
编写celeryconfig.py
CELERY_IMPORTS = ('tasks',)
BROKER_URL = 'amqp://guest@localhost:5672//'
CELERY_RESULT_BACKEND = 'amqp://'
编写spider.py
from tasks import get_html
from queue import Queue
from bs4 import BeautifulSoup
from urllib.parse import urlparse,urljoin
import threading
class spider(object):
def __init__(self):
self.visited={}
self.queue=Queue()
def process_html(self, html):
pass
#print(html)
def _add_links_to_queue(self,url_base,html):
soup = BeautifulSoup(html)
links=soup.find_all('a')
for link in links:
try:
url=link['href']
except:
pass
else:
url_com=urlparse(url)
if not url_com.netloc:
self.queue.put(urljoin(url_base,url))
else:
self.queue.put(url_com.geturl())
def start(self,url):
self.queue.put(url)
for i in range(20):
t = threading.Thread(target=self._worker)
t.daemon = True
t.start()
self.queue.join()
def _worker(self):
while 1:
url=self.queue.get()
if url in self.visited:
continue
else:
result=get_html.delay(url)
try:
html=result.get(timeout=5)
except Exception as e:
print(url)
print(e)
self.process_html(html)
self._add_links_to_queue(url,html)
self.visited[url]=True
self.queue.task_done()
s=spider()
s.start("https://www.jb51.net/")
由于html中某些特殊情况的存在,程序还有待完善。


猜你喜欢
- 在我的电脑中存在多个版本的Python,实际工作中也时常需要切换Python版本来进行相关工作。在Pycharm的终端中使用python和i
- python如何建立一个自己的包一些概念模块:我们写的每个py都是一个模块包:模块的集合,就是一个包,通常包和directory的区别在于是
- 废话不多说。直接上代码:sock_post.php:<?phpfunction sock_post($url, $data='
- The WeekdayName function returns the weekday name of a specified day o
- 本文实例讲述了Python实现获取照片拍摄日期并重命名的方法。分享给大家供大家参考,具体如下:python获取照片的拍摄日期并重命名。不支持
- 前言最近因为工作的原因,在做APP购物车下单支付这一块儿.被测试提了一个bug,当点加入购物车点的比较快的时候,同一个商品在购物车中出现了两
- SecureFile功能是oracle 11g中对大对象(LOB)存储格式的完全重新设计实现,原来的LOB存储格式现在通称为BASIXFIL
- GOPATH设置go 命令依赖一个重要的环境变量:$GOPATH1(注:这个不是Go安装目录。下面以笔者的工作目录为说明,请替换自己机器上的
- 前文主要纠正title用法上的几点误区,其实除链接和表单的常规标签用法。在内容组织方面还有大潜力待发掘,比如写网志经常会有针对词、短语说明的
- 目录1、Go 官方的定义2、传值和传引用2.1 传值2.2 传引用3、争议最大的 map 和 slice3.1 map3.2 slice3、
- 前言Python 提供了很多截取字符串的方法,被称为“切片(slicing)”。模版如下:strin
- 1 装饰器背景知识1.1 基本概念装饰器(Decorator)是 Python 中一种函数或类,用来修饰其他函数或类。装饰器可以改变被装饰函
- 引言阿刁是一个自动化测试用例,从一出生他就被赋予终生使命,去测试一个叫登录的过程是否合理。他一直就被关在一个小黑屋里面,从来也没有出去过,小
- python在mysql中插入null空值sql = “INSERT INTO MROdata (MmeUeS1apId) VALUES (
- PHP程序员玩转Linux系列文章:1.PHP程序员玩转Linux系列-怎么安装使用CentOS2.PHP程序员玩转Linux系列-lnmp
- 一、环境准备1、安装node.js下载地址:https://nodejs.org/zh-cn/界面展示2、检查node.js版本查看版本的两
- str为字符串s为字符串str.isalnum() 所有字符都是数字或者字母str.isalpha() 所有字符都是字母str.isdigi
- PyQt5相关安装python 版本 python 3.6.31、安装PyQt5执行命令: pip install pyqt52、安装PyQ
- 文件操作的步骤:打开文件 -> 操作文件 -> 关闭文件切记:最后要关闭文件(否则可能会有意想不到的结果)打开文件文件句柄 =
- 前言首先来讲一下服务端渲染,直白的说就是在服务端拿数据进行解析渲染,直接生成html片段返回给前端。具体用法也有很多种比如:传统的服务端模板