2

おそらくこのようなものを使用して、セロリタスクワーカー内でスクレイピークローラーを常に実行したいと思います。または、ドキュメントで提案されている ように、クローラーを使用して、XML 応答を返す外部 API を照会することをお勧めします。クローラーに照会したい URL (または照会パラメーターを作成して、クローラーに URL を作成させる) を渡すと、クローラーは URL 呼び出しを行い、抽出されたアイテムを返します。クローラーが実行を開始したら、フェッチしたいこの新しい URL をクローラーに渡すにはどうすればよいですか。新しい URL を指定するたびにクローラーを再起動するのではなく、URL がクロールされるのをクローラーが待機するようにしたいと考えています。

別の python プロセス内でスクレイピーを実行するために私が見つけた 2 つの方法は、新しいプロセスを使用してクローラーを実行します。高価で不要です。

4

2 に答える 2

0

メッセージ キュー ( IronMQのような--完全開示、IronMQ を開発者エバンジェリストとして作成している会社で働いています) を使用して URL を渡すことができます。

次に、クローラーで、キューから URL をポーリングし、取得したメッセージに基づいてクロールします。

リンク先の例は更新される可能性があります(これはテストされておらず、疑似コードですが、基本的な考え方は理解できるはずです):

from twisted.internet import reactor
from scrapy.crawler import Crawler
from scrapy.settings import Settings
from scrapy import log
from testspiders.spiders.followall import FollowAllSpider
from iron-mq import IronMQ

mq = IronMQ()
q = mq.queue("scrape_queue")
crawler = Crawler(Settings())
crawler.configure()
while True: # poll forever
    msg = q.get(timeout=120) # get messages from queue
                             # timeout is the number of seconds the message will be reserved for, making sure no other crawlers get that message. Set it to a safe value (the max amount of time it will take you to crawl a page)
    if len(msg["messages"]) < 1: # if there are no messages waiting to be crawled
        time.sleep(1) # wait one second
        continue # try again
    spider = FollowAllSpider(domain=msg["messages"][0]["body"]) # crawl the domain in the message
    crawler.crawl(spider)
    crawler.start()
    log.start()
    reactor.run() # the script will block here
    q.delete(msg["messages"][0]["id"]) # when you're done with the message, delete it
于 2013-05-24T13:40:38.737 に答える