Python實(shí)現(xiàn)在線程里運(yùn)行scrapy的方法
本文實(shí)例講述了Python實(shí)現(xiàn)在線程里運(yùn)行scrapy的方法。分享給大家供大家參考。具體如下:
如果你希望在一個寫好的程序里調(diào)用scrapy,就可以通過下面的代碼,讓scrapy運(yùn)行在一個線程里。
"""
Code to run Scrapy crawler in a thread - works on Scrapy 0.8
"""
import threading, Queue
from twisted.internet import reactor
from scrapy.xlib.pydispatch import dispatcher
from scrapy.core.manager import scrapymanager
from scrapy.core.engine import scrapyengine
from scrapy.core import signals
class CrawlerThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.running = False
def run(self):
self.running = True
scrapymanager.configure(control_reactor=False)
scrapymanager.start()
reactor.run(installSignalHandlers=False)
def crawl(self, *args):
if not self.running:
raise RuntimeError("CrawlerThread not running")
self._call_and_block_until_signal(signals.spider_closed, \
scrapymanager.crawl, *args)
def stop(self):
reactor.callFromThread(scrapyengine.stop)
def _call_and_block_until_signal(self, signal, f, *a, **kw):
q = Queue.Queue()
def unblock():
q.put(None)
dispatcher.connect(unblock, signal=signal)
reactor.callFromThread(f, *a, **kw)
q.get()
# Usage example below:
import os
os.environ.setdefault('SCRAPY_SETTINGS_MODULE', 'myproject.settings')
from scrapy.xlib.pydispatch import dispatcher
from scrapy.core import signals
from scrapy.conf import settings
from scrapy.crawler import CrawlerThread
settings.overrides['LOG_ENABLED'] = False # avoid log noise
def item_passed(item):
print "Just scraped item:", item
dispatcher.connect(item_passed, signal=signals.item_passed)
crawler = CrawlerThread()
print "Starting crawler thread..."
crawler.start()
print "Crawling somedomain.com...."
crawler.crawl('somedomain.com) # blocking call
print "Crawling anotherdomain.com..."
crawler.crawl('anotherdomain.com') # blocking call
print "Stopping crawler thread..."
crawler.stop()
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
- 實(shí)踐Python的爬蟲框架Scrapy來抓取豆瓣電影TOP250
- 使用Python的Scrapy框架編寫web爬蟲的簡單示例
- Python基于scrapy采集數(shù)據(jù)時使用代理服務(wù)器的方法
- Python使用scrapy采集數(shù)據(jù)時為每個請求隨機(jī)分配user-agent的方法
- Python使用scrapy采集數(shù)據(jù)過程中放回下載過大頁面的方法
- Python使用scrapy采集時偽裝成HTTP/1.1的方法
- Python打印scrapy蜘蛛抓取樹結(jié)構(gòu)的方法
- Python使用scrapy抓取網(wǎng)站sitemap信息的方法
- Python自定義scrapy中間模塊避免重復(fù)采集的方法
- 深入剖析Python的爬蟲框架Scrapy的結(jié)構(gòu)與運(yùn)作流程
相關(guān)文章
基于循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)實(shí)現(xiàn)影評情感分類
這篇文章主要為大家詳細(xì)介紹了基于循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)實(shí)現(xiàn)影評情感分類,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
利用Python腳本寫端口掃描器socket,python-nmap
這篇文章主要介紹了利用Python腳本寫端口掃描器socket,python-nmap,文章圍繞主題展開詳細(xì)介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07
Python個人博客程序開發(fā)實(shí)例框架設(shè)計(jì)
這篇文章主要介紹了怎樣用Java來實(shí)現(xiàn)一個完整的個人博客系統(tǒng),我們通過實(shí)操上手的方式可以高效的鞏固所學(xué)的基礎(chǔ)知識,感興趣的朋友一起來看看吧2022-12-12
python中functools.lru_cache的具體使用
本文主要介紹了python中functools.lru_cache的具體使用,通過functools.lru_cache,你可以輕松優(yōu)化具有重復(fù)計(jì)算的函數(shù),大大提高代碼的執(zhí)行效率2024-09-09
深入理解Python虛擬機(jī)中浮點(diǎn)數(shù)(float)的實(shí)現(xiàn)原理及源碼
在本篇文章當(dāng)中主要分析在 cpython 虛擬機(jī)當(dāng)中 float 類型的實(shí)現(xiàn)原理以及與他相關(guān)的一些源代碼,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-03-03

