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版
Dec 07 Python
Python中用PIL库批量给图片加上序号的教程
May 06 Python
Python提取网页中超链接的方法
Sep 18 Python
python之virtualenv的简单使用方法(必看篇)
Nov 25 Python
python中scikit-learn机器代码实例
Aug 05 Python
django利用request id便于定位及给日志加上request_id
Aug 26 Python
在numpy矩阵中令小于0的元素改为0的实例
Jan 26 Python
Django和Flask框架优缺点对比
Oct 24 Python
python:目标检测模型预测准确度计算方式(基于IoU)
Jan 18 Python
快速查找Python安装路径方法
Feb 06 Python
pytorch随机采样操作SubsetRandomSampler()
Jul 07 Python
Python Flask请求扩展与中间件相关知识总结
Jun 11 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(1)
2006/10/09 PHP
一个简单的PHP&MYSQL留言板源码
2020/07/19 PHP
php数组应用之比较两个时间的相减排序
2008/08/18 PHP
创建数据库php代码 用PHP写出自己的BLOG系统
2010/04/12 PHP
WordPres对前端页面调试时的两个PHP函数使用小技巧
2015/12/22 PHP
PHP中类的继承和用法实例分析
2016/05/24 PHP
ThinkPHP like模糊查询,like多匹配查询,between查询,in查询,一般查询书写方法
2018/09/26 PHP
超级退弹代码
2008/07/07 Javascript
JavaScript 以对象为索引的关联数组
2010/05/19 Javascript
JS从一组数据中找到指定的单条数据的方法
2016/06/02 Javascript
原生js的RSA和AES加密解密算法
2016/10/08 Javascript
浅谈vue自定义全局组件并通过全局方法 Vue.use() 使用该组件
2017/12/07 Javascript
webstorm中vue语法的支持详解
2018/05/09 Javascript
js将URL网址转为16进制加密与解密函数
2020/03/04 Javascript
关于vue属性使用和不使用冒号的区别说明
2020/10/22 Javascript
angular8.5集成TinyMce5的使用和详细配置(推荐)
2020/11/16 Javascript
[01:30:54]《加油DOTA》 第三期
2014/08/18 DOTA
[44:50]2018DOTA2亚洲邀请赛 4.1 小组赛 A组 TNC vs VG
2018/04/02 DOTA
Python实现读取SQLServer数据并插入到MongoDB数据库的方法示例
2018/06/09 Python
python实现视频分帧效果
2019/05/31 Python
Python-while 计算100以内奇数和的方法
2019/06/11 Python
由面试题加深对Django的认识理解
2019/07/19 Python
Python基于xlrd模块处理合并单元格
2020/07/28 Python
详解java调用python的几种用法(看这篇就够了)
2020/12/10 Python
Python使用cn2an实现中文数字与阿拉伯数字的相互转换
2021/03/02 Python
html5本地存储 localStorage操作使用详解
2016/09/20 HTML / CSS
英国轻奢珠宝品牌:Astley Clarke
2016/12/18 全球购物
Hotels.com韩国:海外国内旅行所需的酒店和住宿预订网站
2020/05/08 全球购物
家长通知书教师评语
2014/04/17 职场文书
岗位明星事迹材料
2014/05/18 职场文书
放飞梦想演讲稿600字
2014/08/26 职场文书
儿子满月酒致辞
2015/07/29 职场文书
国庆节主题班会
2015/08/15 职场文书
2019行政前台转正申请书范文3篇
2019/08/15 职场文书
POST提交数据常见的四种方式
2022/01/18 HTML / CSS
Nginx利用Logrotate实现日志分割
2022/05/20 Servers