简体   繁体   English

python-scrapy不遵循链接

[英]python - scrapy doesn't follow links

I'm trying to parse site with Scrapy. 我正在尝试使用Scrapy解析网站。 The urls I need to parse formed like this http://example.com/productID/1234/ . 我需要解析的网址是这样的http://example.com/productID/1234/ This links can be found on pages with address like: http://example.com/categoryID/1234/ . 可以在以下地址的页面上找到此链接: http : //example.com/categoryID/1234/ The thing is that my crawler fetches first categoryID page (http://www.example.com/categoryID/79/, as you can see from trace below), but nothing more. 问题是我的搜寻器获取了第一个categoryID页面(http://www.example.com/categoryID/79/,正如您从下面的跟踪中看到的),但仅此而已。 What am I doing wrong? 我究竟做错了什么? Thank you. 谢谢。

Here is my Scrapy code: 这是我的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)

Here is a trace of Scrapy: 这是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}

It can be a difference between "www.example.com" and "example.com". “ www.example.com”和“ example.com”之间可能有所不同。 If it helps, you can use them both this way 如果有帮助,您可以同时使用它们

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

Replace: 更换:

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

with: 有:

allowed_domains = ["example.com"]

That should do the trick. 这应该够了吧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM