如何在程序中向Scrapy爬虫传递参数?

3

我是Python和Scrapy的新手。我使用了这篇博客中的方法Running multiple scrapy spiders programmatically在Flask应用程序中运行我的蜘蛛。以下是代码:

# list of crawlers
TO_CRAWL = [DmozSpider, EPGDspider, GDSpider]

# crawlers that are running 
RUNNING_CRAWLERS = []

def spider_closing(spider):
    """
    Activates on spider closed signal
    """
    log.msg("Spider closed: %s" % spider, level=log.INFO)
    RUNNING_CRAWLERS.remove(spider)
    if not RUNNING_CRAWLERS:
        reactor.stop()

# start logger
log.start(loglevel=log.DEBUG)

# set up the crawler and start to crawl one spider at a time
for spider in TO_CRAWL:
    settings = Settings()

    # crawl responsibly
    settings.set("USER_AGENT", "Kiran Koduru (+http://kirankoduru.github.io)")
    crawler = Crawler(settings)
    crawler_obj = spider()
    RUNNING_CRAWLERS.append(crawler_obj)

    # stop reactor when spider closes
    crawler.signals.connect(spider_closing, signal=signals.spider_closed)
    crawler.configure()
    crawler.crawl(crawler_obj)
    crawler.start()

# blocks process; so always keep as the last statement
reactor.run()

这是我的爬虫代码:

class EPGDspider(scrapy.Spider):
name = "EPGD"
allowed_domains = ["epgd.biosino.org"]
term = "man"
start_urls = ["http://epgd.biosino.org/EPGD/search/textsearch.jsp?textquery="+term+"&submit=Feeling+Lucky"]
MONGODB_DB = name + "_" + term
MONGODB_COLLECTION = name + "_" + term

def parse(self, response):
    sel = Selector(response)
    sites = sel.xpath('//tr[@class="odd"]|//tr[@class="even"]')
    url_list = []
    base_url = "http://epgd.biosino.org/EPGD"

    for site in sites:
        item = EPGD()
        item['genID'] = map(unicode.strip, site.xpath('td[1]/a/text()').extract())
        item['genID_url'] = base_url+map(unicode.strip, site.xpath('td[1]/a/@href').extract())[0][2:]
        item['taxID'] = map(unicode.strip, site.xpath('td[2]/a/text()').extract())
        item['taxID_url'] = map(unicode.strip, site.xpath('td[2]/a/@href').extract())
        item['familyID'] = map(unicode.strip, site.xpath('td[3]/a/text()').extract())
        item['familyID_url'] = base_url+map(unicode.strip, site.xpath('td[3]/a/@href').extract())[0][2:]
        item['chromosome'] = map(unicode.strip, site.xpath('td[4]/text()').extract())
        item['symbol'] = map(unicode.strip, site.xpath('td[5]/text()').extract())
        item['description'] = map(unicode.strip, site.xpath('td[6]/text()').extract())
        yield item

    sel_tmp = Selector(response)
    link = sel_tmp.xpath('//span[@id="quickPage"]')

    for site in link:
        url_list.append(site.xpath('a/@href').extract())

    for i in range(len(url_list[0])):
        if cmp(url_list[0][i], "#") == 0:
            if i+1 < len(url_list[0]):
                print url_list[0][i+1]
                actual_url = "http://epgd.biosino.org/EPGD/search/"+ url_list[0][i+1]
                yield Request(actual_url, callback=self.parse)
                break
            else:
                print "The index is out of range!"

从我的代码中可以看到,有一个参数term = 'man',它是我的start urls的一部分。我不希望这个参数是固定的,所以我想知道如何在程序中动态地给出start url或参数term。就像在命令行中运行蜘蛛爬虫一样,有一种方法可以传递参数,如下所示:

class MySpider(BaseSpider):

    name = 'my_spider'    

    def __init__(self, *args, **kwargs): 
      super(MySpider, self).__init__(*args, **kwargs) 

      self.start_urls = [kwargs.get('start_url')] 
And start it like: scrapy crawl my_spider -a start_url="http://some_url"

有谁能告诉我如何处理这个问题?


是的,scrapy crawl my_spider -a start_url="http://google.com" 运行良好。 - Padraic Cunningham
但我不想在命令行中调用我的爬虫,我想在我的程序中调用爬虫。 - Coding_Rabbit
1个回答

12
首先,在脚本中运行多个爬虫的推荐方法是使用 scrapy.crawler.CrawlerProcess其中传递的是爬虫类而不是爬虫实例。 要使用 CrawlerProcess 为你的爬虫传递参数,只需要在 .crawl() 调用之后添加这些参数,例如:
    process.crawl(DmozSpider, term='someterm', someotherterm='anotherterm')

这样传递的参数就会作为爬虫属性可用(与在命令行中使用-a term=someterm相同)。

最后,你可以使用start_requests代替在__init__中构建start_urls,并且你可以像这样使用self.term来构建初始请求:

def start_requests(self):
    yield Request("http://epgd.biosino.org/"
                  "EPGD/search/textsearch.jsp?"
                  "textquery={}"
                  "&submit=Feeling+Lucky".format(self.term))

问题在于我在 Flask 应用程序中运行这些爬虫,所以我应该尝试使用 scrapy.crawler.Crawler 而不是 CrawlerProcess - Coding_Rabbit
我不知道如何在Flask应用程序中运行scrapy爬虫。我会问一下。 - paul trmbrth
我发现我使用的是 scrapy - 0.24.0 而不是 scrapy -1.0,在 scrapy - 0.24.0 中,Crawler 只有一个参数 settings,与最新版本略有不同。 - Coding_Rabbit
好的,那么请查看0.24文档,在一个传递参数的蜘蛛示例中。 - paul trmbrth
抱歉,我已经阅读了这份官方文档,但是我没有看到有一个传递参数的示例...我也尝试了 scrapy -1.0 中的 Crawler Runner 方法,不知道 Crawler Runner 方法是否有传递参数的方式? - Coding_Rabbit
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接