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

时间:2022-04-20 20:11:58 

编写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,rabbitmq,网络爬虫
0
投稿

猜你喜欢

  • 教你轻松学会SQL Server记录轮班的技巧

    2009-02-19 17:38:00
  • ASP使用组件在线发E-mail的2个函数

    2008-06-12 07:13:00
  • asp如何获知文件最后的修改日期和时间?

    2009-11-24 20:49:00
  • 不用Global.asa也能实现统计在线人数吗?

    2009-10-29 12:28:00
  • asp.net“服务器应用程序不可用” 解决方法

    2023-06-29 10:05:30
  • 教程:打造SQL Server2000的安全策略

    2008-12-23 15:52:00
  • redis查看连接数及php模拟并发创建redis连接的方法

    2023-11-16 11:47:14
  • PHP 页面跳转到另一个页面的多种方法方法总结

    2023-06-14 21:05:49
  • asp下为什么韩文字后面显示分号?

    2011-03-10 11:07:00
  • php让json_encode不自动转义斜杠“/”的方法

    2023-08-19 17:04:28
  • Jquery作者John Resig自己封装的javascript 常用函数

    2023-09-11 13:14:16
  • apache配置虚拟主机的方法详解

    2023-06-18 09:05:29
  • php文件怎么打开 如何执行php文件

    2023-11-15 05:51:46
  • 通过VB6将ASP编译封装成DLL组件最简教程 附全部工程源文件

    2012-11-30 20:20:50
  • Javascript与PHP验证用户输入URL地址是否正确

    2023-10-23 02:36:07
  • (X)HTML的文档结构

    2008-06-30 12:25:00
  • 如何防止未经注册的用户绕过注册界面直接进入应用系统?

    2009-11-22 19:22:00
  • 仿淘宝网站的导航标签效果!

    2008-11-05 12:37:00
  • 深入浅析Python代码规范性检测

    2023-08-23 21:16:55
  • 如何优化下面这段代码?

    2010-01-23 11:30:00
  • asp之家 网络编程 m.aspxhome.com