python使用rabbitmq实现网络爬虫示例


Posted in Python onFebruary 20, 2014

编写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://3water.com/")

由于html中某些特殊情况的存在,程序还有待完善。

Python 相关文章推荐
Python字符转换
Sep 06 Python
Python正则表达式教程之三:贪婪/非贪婪特性
Mar 02 Python
python下读取公私钥做加解密实例详解
Mar 29 Python
Python实现的寻找前5个默尼森数算法示例
Mar 25 Python
Python实现制度转换(货币,温度,长度)
Jul 14 Python
Python Web版语音合成实例详解
Jul 16 Python
解决Mac下使用python的坑
Aug 13 Python
pygame实现飞机大战
Mar 11 Python
使用pygame编写Flappy bird小游戏
Mar 14 Python
python使用OpenCV模块实现图像的融合示例代码
Apr 10 Python
详解利用python识别图片中的条码(pyzbar)及条码图片矫正和增强
Nov 17 Python
pytorch 如何使用batch训练lstm网络
May 28 Python
python使用win32com在百度空间插入html元素示例
Feb 20 #Python
python基础教程之类class定义使用方法
Feb 20 #Python
python基础教程之基本内置数据类型介绍
Feb 20 #Python
python实现dict版图遍历示例
Feb 19 #Python
使用python在校内发人人网状态(人人网看状态)
Feb 19 #Python
下载给定网页上图片的方法
Feb 18 #Python
使用python将mdb数据库文件导入postgresql数据库示例
Feb 17 #Python
You might like
一段防盗连的PHP代码
2006/12/06 PHP
简单的php 验证图片生成函数
2009/05/21 PHP
深入解析php之sphinx
2013/05/15 PHP
php有道翻译api调用方法实例
2014/12/22 PHP
Yii rules常用规则示例
2016/03/15 PHP
详解PHP编码转换函数应用技巧
2016/10/22 PHP
微信公众平台开发教程⑥ 微信开发集成类的使用图文详解
2019/04/10 PHP
js关闭模态窗口刷新父页面或跳转页面
2012/12/13 Javascript
jQuery中bind与live的用法及区别小结
2014/01/27 Javascript
jquery中$.post()方法的简单实例
2014/02/04 Javascript
js中document.write使用过程中的一点疑问解答
2014/03/20 Javascript
Ext GridPanel加载完数据后进行操作示例代码
2014/06/17 Javascript
js 操作符汇总
2014/11/08 Javascript
JavaScript实现带缓冲效果的随屏滚动漂浮广告代码
2015/11/06 Javascript
JavaScript利用HTML DOM进行文档操作的方法
2016/03/28 Javascript
微信小程序小组件 基于Canvas实现直播点赞气泡效果
2020/05/29 Javascript
JavaScript代码判断输入的字符串是否含有特殊字符和表情代码实例
2017/08/17 Javascript
微信小程序跨页面数据传递事件响应实现过程解析
2019/12/19 Javascript
JavaScript中break、continue和return的用法区别实例分析
2020/03/02 Javascript
Python代码一键转Jar包及Java调用Python新姿势
2020/03/10 Python
浅谈Python 参数与变量
2020/06/20 Python
python获取本周、上周、本月、上月及本季的时间代码实例
2020/09/08 Python
CSS3感应鼠标的背景闪烁和图片缩放动画效果
2014/05/14 HTML / CSS
加拿大大码女装购物网站:Penningtons
2020/12/26 全球购物
J2EE系统只能是基于web
2015/09/08 面试题
老师给学生的表扬信
2014/01/17 职场文书
卫生巾广告词
2014/03/18 职场文书
教师见习期自我鉴定
2014/04/28 职场文书
2014年会计工作总结
2014/11/27 职场文书
社区公民道德宣传日活动总结
2015/03/23 职场文书
给校长的建议书作文400字
2015/09/14 职场文书
大学生党员暑假实践(活动总结)
2019/08/21 职场文书
幼师必备:幼儿园期末教师评语50条
2019/11/01 职场文书
SQL注入的实现以及防范示例详解
2021/06/02 MySQL
MySQL 用 limit 为什么会影响性能
2021/09/15 MySQL
深入讲解Vue中父子组件通信与事件触发
2022/03/22 Vue.js