开发者

Why does scrapy throw an error for me when trying to spider and parse a site?

开发者 https://www.devze.com 2023-02-16 06:08 出处:网络
The following code class SiteSpider(BaseSpider): name = \"some_site.com\" allowed_domains = [\"some_site.com\"]

The following code

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

Throws the following error

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/开发者_JAVA技巧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: 

When I change the callback to "parse" and the function to "parse" i don't get any errors, but nothing is scraped. I changed it to "parse_items" thinking I might be overriding the parse method by accident. Perhaps I'm setting up the link extractor wrong?

What I want to do is parse each ITEM link on the CATEGORY page. Am I doing this totally wrong?


I needed to change BaseSpider to CrawlSpider. Thanks srapy users!

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

Hi Bob,

Perhaps it might work if you change from BaseSpider to CrawlSpider? The BaseSpider seems not implement Rule, see:

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

-M


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消