6

以下代码

class SiteSpider(BaseSpider):
    name = "some_site.com"
    allowed_domains = ["some_site.com"]
    start_urls = [
        "some_site.com/something/another/PRODUCT-CATEGORY1_10652_-1__85667",
    ]
    rules = (
        Rule(SgmlLinkExtractor(allow=('some_site.com/something/another/PRODUCT-CATEGORY_(.*)', ))),

        # Extract links matching 'item.php' and parse them with the spider's method parse_item
        Rule(SgmlLinkExtractor(allow=('some_site.com/something/another/PRODUCT-DETAIL(.*)', )), callback="parse_item"),
    )
    def parse_item(self, response):
.... parse stuff

抛出以下错误

Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 1174, in mainLoop
    self.runUntilCurrent()
  File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 796, in runUntilCurrent
    call.func(*call.args, **call.kw)
  File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 318, in callback
    self._startRunCallbacks(result)
  File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 424, in _startRunCallbacks
    self._runCallbacks()
--- <exception caught here> ---
  File "/usr/lib/python2.6/dist-packages/twisted/internet/defer.py", line 441, in _runCallbacks
    self.result = callback(self.result, *args, **kw)
  File "/usr/lib/pymodules/python2.6/scrapy/spider.py", line 62, in parse
    raise NotImplementedError
exceptions.NotImplementedError: 

当我将回调更改为“解析”并将函数更改为“解析”时,我没有收到任何错误,但没有任何内容被刮掉。我将其更改为“parse_items”,认为我可能会意外覆盖 parse 方法。也许我设置的链接提取器错误?

我想要做的是解析 CATEGORY 页面上的每个 ITEM 链接。我这样做完全错了吗?

4

2 回答 2

9

我需要将 BaseSpider 更改为 CrawlSpider。感谢srapy用户!

http://groups.google.com/group/scrapy-users/browse_thread/thread/4adaba51f7bcd0af#

嗨,鲍勃,

如果您从 BaseSpider 更改为 CrawlSpider,也许它可能会起作用?BaseSpider 似乎没有实现规则,请参阅:

http://doc.scrapy.org/topics/spiders.html?highlight=rule#scrapy.contr...

-M

于 2011-03-11T16:55:42.063 回答
4

By default scrapy searches for parse function in the class. Here in your spider, parse function is missing. Instead of parse you have given parse_item. The problem will be solved if parse_item is replace with parse. Or you can override the parse method in spider.py with that of parse_item.

于 2015-01-31T06:01:48.417 回答