3

我在让我的 Scrapy 蜘蛛运行它的回调方法时遇到问题。

我不认为这是一个缩进错误,这似乎是以前其他帖子的情况,但也许是这样,我不知道吗?有任何想法吗?

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy import log
import tldextract

class CrawlerSpider(CrawlSpider):
  name = "crawler"

  def __init__(self, initial_url):
    log.msg('initing...', level=log.WARNING)
    CrawlSpider.__init__(self)

    if not initial_url.startswith('http'):
      initial_url = 'http://' + initial_url

    ext = tldextract.extract(initial_url)
    initial_domain = ext.domain + '.' + ext.tld
    initial_subdomain = ext.subdomain + '.' + ext.domain + '.' + ext.tld
    self.allowed_domains = [initial_domain, 'www.' + initial_domain, initial_subdomain]
    self.start_urls = [initial_url]
    self.rules = [
        Rule(SgmlLinkExtractor(), callback='parse_item'),
        Rule(SgmlLinkExtractor(allow_domains=self.allowed_domains), follow=True),
    ]
    self._compile_rules()

  def parse_item(self, response):
    log.msg('parse_item...', level=log.WARNING)
    hxs = HtmlXPathSelector(response)
    links = hxs.select("//a/@href").extract()
    for link in links:
      log.msg('link', level=log.WARNING)

示例输出如下;它应该显示一条带有“parse_item ...”的警告消息,但它没有。

$ scrapy crawl crawler -a initial_url=http://www.szuhanchang.com/test.html
2013-02-19 18:03:24+0000 [scrapy] INFO: Scrapy 0.16.4 started (bot: crawler)
2013-02-19 18:03:24+0000 [scrapy] DEBUG: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, SpiderState
2013-02-19 18:03:24+0000 [scrapy] DEBUG: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, RedirectMiddleware, CookiesMiddleware, HttpCompressionMiddleware, ChunkedTransferMiddleware, DownloaderStats
2013-02-19 18:03:24+0000 [scrapy] DEBUG: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2013-02-19 18:03:24+0000 [scrapy] DEBUG: Enabled item pipelines: 
2013-02-19 18:03:24+0000 [scrapy] WARNING: initing...
2013-02-19 18:03:24+0000 [crawler] INFO: Spider opened
2013-02-19 18:03:24+0000 [crawler] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2013-02-19 18:03:24+0000 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023
2013-02-19 18:03:24+0000 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2013-02-19 18:03:25+0000 [crawler] DEBUG: Crawled (200) <GET http://www.szuhanchang.com/test.html> (referer: None)
2013-02-19 18:03:25+0000 [crawler] DEBUG: Filtered offsite request to 'www.20130219-0606.com': <GET http://www.20130219-0606.com/>
2013-02-19 18:03:25+0000 [crawler] INFO: Closing spider (finished)
2013-02-19 18:03:25+0000 [crawler] INFO: Dumping Scrapy stats:
        {'downloader/request_bytes': 234,
         'downloader/request_count': 1,
         'downloader/request_method_count/GET': 1,
         'downloader/response_bytes': 363,
         'downloader/response_count': 1,
         'downloader/response_status_count/200': 1,
         'finish_reason': 'finished',
         'finish_time': datetime.datetime(2013, 2, 19, 18, 3, 25, 84855),
         'log_count/DEBUG': 8,
         'log_count/INFO': 4,
         'log_count/WARNING': 1,
         'request_depth_max': 1,
         'response_received_count': 1,
         'scheduler/dequeued': 1,
         'scheduler/dequeued/memory': 1,
         'scheduler/enqueued': 1,
         'scheduler/enqueued/memory': 1,
         'start_time': datetime.datetime(2013, 2, 19, 18, 3, 24, 805064)}
2013-02-19 18:03:25+0000 [crawler] INFO: Spider closed (finished)

提前致谢!

4

2 回答 2

4

http://www.szuhanchang.com/test.html的只有一个锚链接,即start_urls

<a href="http://www.20130219-0606.com">Test</a>

其中包含指向域的链接,20130219-0606.com并根据您allowed_domains的:

['szuhanchang.com', 'www.szuhanchang.com', 'www.szuhanchang.com']

此请求被过滤OffsiteMiddleware

2013-02-19 18:03:25+0000 [crawler] DEBUG: Filtered offsite request to 'www.20130219-0606.com': <GET http://www.20130219-0606.com/>

因此parse_item不会为此 url 调用。

于 2013-02-19T20:02:27.843 回答
1

将回调的名称更改为parse_start_url似乎可行,尽管由于提供的测试 URL 很小,我不能确定这是否仍然有效。试一试,让我知道。:)

于 2013-02-19T18:54:01.483 回答