2015-04-15 39 views
-1

我想創建一個能夠在每個網站上的url爬行器之間跳轉的bot。 enter image description here使用scrapy跳到爬網url

如果在網站上有兩個網址,我想開始2個新的實例抓取那裏的網站。

可能同時限制實例的個數。

我的代碼實際上只適用於1個網站,它無法跳轉到獲取網址。

from scrapy.contrib.spiders import CrawlSpider, Rule 
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor 
from scrapy.selector import HtmlXPathSelector 
from scrapy.item import Item 
from scrapy.spider import BaseSpider 
from scrapy import log 

class ExampleSpider(CrawlSpider): 
    name = "crawler" 
    allowed_domains = ["*"] 
    start_urls = ["http://domainA.com"] 
    rules = [Rule(SgmlLinkExtractor(), callback='parse_item', follow=True)] 
    def parse_item(self,response): 
     self.log('A response from %s just arrived!' % response.url) 
+0

你能提供一個真實的世界reproduceable例子關注的是你的期望的輸出是什麼?謝謝。 – alecxe

+0

您可以使用工作池(在這種情況下,您可以使用隊列)。因此,您有一個負責創建作業(要爬網的URL)並將其插入隊列的進程。然後,您可以使用多個線程從池中獲取工作,因爲隊列在它們之間共享。 –

+0

@alecxe放入數據庫過期的域名。 – Pixel

回答

0

您可以定義,如果它通過與子(https://docs.python.org/2/library/subprocess.html)每次你得到的鏈接產生新的進程,成爲起始URL的參數。

也許是這樣的: (N.B - 未測試)

class ExampleSpider(CrawlSpider): 
    name = "test_crawler" 
    allowed_domains = ["*"] 
    rules=[Rule(SgmlLinkExtractor(),callback='parse_item',follow=True)] 

    def __init__(self, starting_url=None): 

     if starting_url: 
      self.start_urls = [starting_url] 
     else: 
      self.start_urls = ["http://www.domainA.com"] 

     CrawlSpider.__init__(self) 


    def parse_item(self,response): 
     subprocess.call('scrapy crawl {0} -a starting_url {1}'.format(self.name, response.url).split())