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实现巡检系统(solaris)示例
Apr 02 Python
django1.8使用表单上传文件的实现方法
Nov 04 Python
Python实现遍历目录的方法【测试可用】
Mar 22 Python
python实现装饰器、描述符
Feb 28 Python
关于python2 csv写入空白行的问题
Jun 22 Python
对Python通过pypyodbc访问Access数据库的方法详解
Oct 27 Python
用Python获取摄像头并实时控制人脸的实现示例
Jul 11 Python
Django 实现admin后台显示图片缩略图的例子
Jul 28 Python
在pandas中遍历DataFrame行的实现方法
Oct 23 Python
Python列表list操作相关知识小结
Jan 29 Python
浅析Python面向对象编程
Jul 10 Python
PyCharm vs VSCode,作为python开发者,你更倾向哪种IDE呢?
Aug 17 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中防止直接访问或查看或下载config.php文件的方法
2012/07/07 PHP
领悟php接口中interface存在的意义
2013/06/27 PHP
ThinkPHP页面跳转success与error方法概述
2014/06/25 PHP
PHP使用mysql_fetch_object从查询结果中获取对象集的方法
2015/03/18 PHP
PHP list() 将数组中的值赋给变量的简单实例
2016/06/13 PHP
微信支付的开发流程详解
2016/09/13 PHP
详谈 Jquery Ajax异步处理Json数据.
2011/09/09 Javascript
jQuery实现自定义右键菜单的树状菜单效果
2015/09/02 Javascript
基于jquery实现图片放大功能
2016/05/07 Javascript
JavaScript代码实现图片循环滚动效果
2020/03/19 Javascript
分享一个原生的JavaScript拖动方法
2016/09/25 Javascript
微信小程序 网络API 上传、下载详解
2016/11/09 Javascript
Bootstrap modal 多弹窗之叠加关闭阴影遮罩问题的解决方法
2017/02/27 Javascript
微信小程序开发入门基础教程
2017/04/19 Javascript
基于Vue实现支持按周切换的日历
2020/09/24 Javascript
iframe高度自适应及隐藏滚动条的实例详解
2017/09/29 Javascript
原生js实现省市区三级联动代码分享
2018/02/12 Javascript
详解webpack-dev-server使用方法
2018/09/14 Javascript
微信小程序框架wepy之动态控制类名
2018/09/14 Javascript
vue中上传视频或图片或图片和文字一起到后端的解决方法
2019/12/01 Javascript
vue 子组件修改data或调用操作
2020/08/07 Javascript
node.js通过Sequelize 连接MySQL的方法
2020/12/28 Javascript
python中的字典详细介绍
2014/09/18 Python
PyQt5固定窗口大小的方法
2019/06/18 Python
python中pytest收集用例规则与运行指定用例详解
2019/06/27 Python
django框架模板语言使用方法详解
2019/07/18 Python
Python matplotlib绘制饼状图功能示例
2019/09/10 Python
详解Django CAS 解决方案
2019/10/30 Python
python脚本实现mp4中的音频提取并保存在原目录
2020/02/27 Python
Python描述数据结构学习之哈夫曼树篇
2020/09/07 Python
CSS3打造磨砂玻璃背景效果
2016/09/28 HTML / CSS
全球性的女装店:storets
2019/06/12 全球购物
linux面试题参考答案(8)
2016/04/19 面试题
警察正风肃纪剖析材料
2014/10/16 职场文书
幼儿园小班工作总结2015
2015/04/25 职场文书
汽车车尾标语大全
2015/08/11 职场文书