python - scrapy不遵循链接

时间:2012-01-31 13:05:11

标签: python scrapy

我正在尝试使用Scrapy解析网站。我需要解析的网址形成为http://example.com/productID/1234/。此链接可在地址为http://example.com/categoryID/1234/的网页上找到。问题是我的抓取工具抓取了第一个类别ID页面(http://www.example.com/categoryID/79/,正如您可以从下面的跟踪中看到的那样),但仅此而已。我究竟做错了什么?谢谢。

这是我的Scrapy代码:

# -*- coding: UTF-8 -*-

#THIRD-PARTY MODULES
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector

class ExampleComSpider(CrawlSpider):
    name = "example.com"
    allowed_domains = ["http://www.example.com/"]

    start_urls = [
        "http://www.example.com/"
    ]

    rules = (
        # Extract links matching 'categoryID/xxx'
        # and follow links from them (since no callback means follow=True by default).
        Rule(SgmlLinkExtractor(allow=('/categoryID/(\d*)/', ), )),

        # Extract links matching 'productID/xxx' and parse them with the spider's method parse_item
        Rule(SgmlLinkExtractor(allow=('/productID/(\d*)/', )), callback='parse_item'),
    )

    def parse_item(self, response):

        self.log('Hi, this is an item page! %s' % response.url)

这是一段Scrapy:

2012-01-31 12:38:56+0000 [scrapy] INFO: Scrapy 0.14.1 started (bot: parsers)
2012-01-31 12:38:57+0000 [scrapy] DEBUG: Enabled extensions: LogStats, TelnetConsole, CloseSpider, WebService, CoreStats, MemoryUsage, SpiderState
2012-01-31 12:38:57+0000 [scrapy] DEBUG: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, RedirectMiddleware, CookiesMiddleware, HttpCompressionMiddleware, ChunkedTransferMiddleware, DownloaderStats
2012-01-31 12:38:57+0000 [scrapy] DEBUG: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2012-01-31 12:38:57+0000 [scrapy] DEBUG: Enabled item pipelines:
2012-01-31 12:38:57+0000 [example.com] INFO: Spider opened
2012-01-31 12:38:57+0000 [example.com] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2012-01-31 12:38:57+0000 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023
2012-01-31 12:38:57+0000 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2012-01-31 12:38:58+0000 [example.com] DEBUG: Crawled (200) <GET http://www.example.com/> (referer: None)
2012-01-31 12:38:58+0000 [example.com] DEBUG: Filtered offsite request to 'www.example.com': <GET http://www.example.com/categoryID/79/>
2012-01-31 12:38:58+0000 [example.com] INFO: Closing spider (finished)
2012-01-31 12:38:58+0000 [example.com] INFO: Dumping spider stats:
  {'downloader/request_bytes': 199,
   'downloader/request_count': 1,
   'downloader/request_method_count/GET': 1,
   'downloader/response_bytes': 121288,
   'downloader/response_count': 1,
   'downloader/response_status_count/200': 1,
   'finish_reason': 'finished',
   'finish_time': datetime.datetime(2012, 1, 31, 12, 38, 58, 409806),
   'request_depth_max': 1,
   'scheduler/memory_enqueued': 1,
   'start_time': datetime.datetime(2012, 1, 31, 12, 38, 57, 127805)}
2012-01-31 12:38:58+0000 [example.com] INFO: Spider closed (finished)
2012-01-31 12:38:58+0000 [scrapy] INFO: Dumping global stats:
  {'memusage/max': 26992640, 'memusage/startup': 26992640}

2 个答案:

答案 0 :(得分:4)

这可能是&#34; www.example.com&#34;和&#34; example.com&#34;。如果它有帮助,你可以这样使用它们

allowed_domains = ["www.example.com", "example.com"]

答案 1 :(得分:1)

替换:

allowed_domains = ["http://www.example.com/"]

使用:

allowed_domains = ["example.com"]

这应该可以解决问题。