【问题标题】:python scrapy unable to find spider while trying to use argumentspython scrapy在尝试使用参数时无法找到蜘蛛
【发布时间】:2012-10-09 09:52:29
【问题描述】:

我已经成功地创建了一个蜘蛛,它可以为域的每个网页检索链接。

我想做同样的事情,但是对于我托管的多个域,为此,我更喜欢使用我的蜘蛛,只需将它作为参数添加到要监视的域。

文档here 解释说我们应该明确定义构造函数并在其中添加参数,然后使用命令scrapy crawl myspider 启动蜘蛛。

这是我的代码:

class MySpider(BaseSpider):
    name= 'spider'

    def __init__(self, domain='some_domain.net'):
        self.domain = domain
        self.allowed_domains = [self.domain]
        self.start_urls = [ 'http://'+self.domain ]

    def parse(self, response):
        hxs = HtmlPathSelector(response)
        for url in hxs.select('//a/@href').extract():
            if not url.startswith('http://'):
                url= URL + url 
            print url
        yield Request(url, callback=self.parse)

但是,启动

scrapy crawl spider -a domain='mydomain.my_extension'

返回:

ERROR: unable to find spider: spider

当我启动相同的代码,但没有显式构造函数时,我无法通过 crawl 来执行此操作,我必须改用这个命令:

scrapy runspider /path/to/spider/spider.py

而且我不能在runspider中使用参数,我必须运行 crawl

为什么不能使用scrapy crawl spider? 为什么scrapy crawl 永远不会读取Spider 的名字,就像scrapy runspider 一样?

Scrapy 看起来很棒,但第二眼看起来很令人不安:/

非常感谢您的帮助

【问题讨论】:

    标签: python scrapy


    【解决方案1】:

    如果你运行 scrapy 0.14 你应该在类级别而不是在实例级别设置变量。我认为这在 0.15 发生了变化

    我建议阅读文档:http://doc.scrapy.org/en/0.14/topics/spiders.html

    class MySpider(BaseSpider):
            name= 'spider'
            domain = domain
            allowed_domains = [self.domain]
            start_urls = [ 'http://'+self.domain ]
    
    
        def parse(self, response):
            hxs = HtmlPathSelector(response)
            for url in hxs.select('//a/@href').extract():
                if not url.startswith('http://'):
                    url= URL + url 
                print url
            yield Request(url, callback=self.parse)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      相关资源
      最近更新 更多