python使用rabbitmq實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(chóng)示例
編寫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("http://m.fzitv.net/")
由于html中某些特殊情況的存在,程序還有待完善。
- Python網(wǎng)絡(luò)爬蟲(chóng)神器PyQuery的基本使用教程
- Python爬蟲(chóng)實(shí)例_城市公交網(wǎng)絡(luò)站點(diǎn)數(shù)據(jù)的爬取方法
- Python3網(wǎng)絡(luò)爬蟲(chóng)之使用User Agent和代理IP隱藏身份
- Python網(wǎng)絡(luò)爬蟲(chóng)出現(xiàn)亂碼問(wèn)題的解決方法
- Python網(wǎng)絡(luò)爬蟲(chóng)實(shí)例講解
- python3使用urllib模塊制作網(wǎng)絡(luò)爬蟲(chóng)
- 詳解Python網(wǎng)絡(luò)爬蟲(chóng)功能的基本寫法
- 以Python的Pyspider為例剖析搜索引擎的網(wǎng)絡(luò)爬蟲(chóng)實(shí)現(xiàn)方法
- 使用Python編寫簡(jiǎn)單網(wǎng)絡(luò)爬蟲(chóng)抓取視頻下載資源
- Python發(fā)展史及網(wǎng)絡(luò)爬蟲(chóng)
相關(guān)文章
Python實(shí)現(xiàn)讀取HTML表格 pd.read_html()
這篇文章主要介紹了Python實(shí)現(xiàn)讀取HTML表格 pd.read_html(),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
pytorch加載的cifar10數(shù)據(jù)集過(guò)程詳解
這篇文章主要介紹了pytorch加載的cifar10數(shù)據(jù)集,到底有沒(méi)有經(jīng)過(guò)歸一化,本文對(duì)這一問(wèn)題給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11
Python的collections模塊中的OrderedDict有序字典
字典是無(wú)序的,但是collections的OrderedDict類為我們提供了一個(gè)有序的字典結(jié)構(gòu),名副其實(shí)的Ordered+Dict,下面通過(guò)兩個(gè)例子來(lái)簡(jiǎn)單了解下Python的collections模塊中的OrderedDict有序字典:2016-07-07
源碼解析python中randint函數(shù)的效率缺陷
這篇文章主要介紹了源碼解析python中randint函數(shù)的效率缺陷,通過(guò)討論?random?模塊的實(shí)現(xiàn),并討論了一些更為快速的生成偽隨機(jī)整數(shù)的替代方法展開(kāi)主題,需要的盆友可以參考一下2022-06-06
如何用Python繪制簡(jiǎn)易動(dòng)態(tài)圣誕樹(shù)
這篇文章主要給大家介紹了關(guān)于如何用Python繪制簡(jiǎn)易動(dòng)態(tài)圣誕樹(shù),文中講解了如何通過(guò)編寫代碼來(lái)實(shí)現(xiàn)特定的效果,包括代碼的編寫技巧和效果的展示,需要的朋友可以參考下2025-01-01
Python+tkinter使用80行代碼實(shí)現(xiàn)一個(gè)計(jì)算器實(shí)例
這篇文章主要介紹了Python+tkinter使用80行代碼實(shí)現(xiàn)一個(gè)計(jì)算器實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
Python深入分析@property裝飾器的應(yīng)用
這篇文章主要介紹了Python @property裝飾器的用法,在Python中,可以通過(guò)@property裝飾器將一個(gè)方法轉(zhuǎn)換為屬性,從而實(shí)現(xiàn)用于計(jì)算的屬性,下面文章圍繞主題展開(kāi)更多相關(guān)詳情,感興趣的小伙伴可以參考一下2022-07-07
Python3+Django get/post請(qǐng)求實(shí)現(xiàn)教程詳解
這篇文章主要介紹了Python3+Django get/post請(qǐng)求實(shí)現(xiàn)教程詳解,需要的朋友可以參考下2021-02-02

