python - 带有 scrapy 的递归蜘蛛只抓取第一页

标签 python scrapy

我正在尝试使用 scrapy 编写一个递归抓取整个站点的蜘蛛。

然而,虽然它似乎很好地抓取了第一页,但它随后找到了该页面上的链接,但没有跟随它们并抓取那些页面,这正是我需要的。

我创建了一个 scrapy 项目并开始编写一个如下所示的蜘蛛:

# -*- coding: utf-8 -*-
import scrapy
from scrapy.http import Request
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from urlparse import urljoin

class EventsSpider(scrapy.Spider):
    name = "events"
    allowed_domains = ["www.foo.bar/"]
    start_urls = (
        'http://www.foo.bar/events/',
        )
    rules = (
        Rule(LinkExtractor(), callback="parse", follow= True),
        )

    def parse(self, response):
        yield {
        'url':response.url,
        'language':response.xpath('//meta[@name=\'Language\']/@content').extract(),
        'description':response.xpath('//meta[@name=\'Description\']/@content').extract(),
        }
        for url in response.xpath('//a/@href').extract():
            if url and not url.startswith('#'):
                self.logger.debug(urljoin(response.url, url))
                scrapy.http.Request(urljoin(response.url, url))

然后,在使用scrapy crawl events -o events.json

运行蜘蛛时

我在以下控制台中得到输出:

2016-05-16 09:50:04 [scrapy] INFO: Spider closed (finished)
PS C:\Projects\foo\src\Scrapy> scrapy crawl events -o .\events.json
2016-05-16 09:54:36 [scrapy] INFO: Scrapy 1.1.0 started (bot: foo)
2016-05-16 09:54:36 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'foo.spiders', 'FEED_URI': '.\\events.json
', 'SPIDER_MODULES': ['foo.spiders'], 'BOT_NAME': 'foo', 'ROBOTSTXT_OBEY': True, 'FEED_FORMAT': 'json'}
2016-05-16 09:54:36 [scrapy] INFO: Enabled extensions:
['scrapy.extensions.feedexport.FeedExporter',
 'scrapy.extensions.logstats.LogStats',
 'scrapy.extensions.telnet.TelnetConsole',
 'scrapy.extensions.corestats.CoreStats']
2016-05-16 09:54:36 [scrapy] INFO: Enabled downloader middlewares:
['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
 'scrapy.downloadermiddlewares.chunked.ChunkedTransferMiddleware',
 'scrapy.downloadermiddlewares.stats.DownloaderStats']
2016-05-16 09:54:36 [scrapy] INFO: Enabled spider middlewares:
['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
 'scrapy.spidermiddlewares.referer.RefererMiddleware',
 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
 'scrapy.spidermiddlewares.depth.DepthMiddleware']
2016-05-16 09:54:36 [scrapy] INFO: Enabled item pipelines:
[]
2016-05-16 09:54:36 [scrapy] INFO: Spider opened
2016-05-16 09:54:36 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2016-05-16 09:54:36 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2016-05-16 09:54:36 [scrapy] DEBUG: Crawled (200) <GET http://www.foo.co.uk/robots.txt> (referer: None)
2016-05-16 09:54:37 [scrapy] DEBUG: Crawled (200) <GET http://www.foo.co.uk/events/> (referer: None)
2016-05-16 09:54:37 [scrapy] DEBUG: Scraped from <200 http://www.foo.co.uk/events/>
{'description': [], 'language': [u'en_UK'], 'url': 'http://www.foo.co.uk/events/'}
2016-05-16 09:54:37 [events] DEBUG: http://www.foo.co.uk/default.aspx
2016-05-16 09:54:37 [events] DEBUG: http://www.foo.co.uk/page/a-z/
2016-05-16 09:54:37 [events] DEBUG: http://www.foo.co.uk/thing/
2016-05-16 09:54:37 [events] DEBUG: http://www.foo.co.uk/other-thing/
2016-05-16 09:54:37 [events] DEBUG: http://www.foo.co.uk/foo-about-us/
2016-05-16 09:54:37 [events] DEBUG: http://www.foo.co.uk/contactus
2016-05-16 09:54:37 [events] DEBUG: http://www.foo.co.uk/events/bar
2016-05-16 09:54:37 [events] DEBUG: http://www.foo.co.uk/events/event
2016-05-16 09:54:37 [events] DEBUG: http://www.foo.co.uk/events/super-cool-party
2016-05-16 09:54:37 [events] DEBUG: http://www.foo.co.uk/events/another-event
2016-05-16 09:54:37 [events] DEBUG: http://www.foo.co.uk/events/more-events
2016-05-16 09:54:37 [events] DEBUG: http://www.foo.co.uk/events/tps-report-convention
...

more links

...

2016-05-16 09:54:37 [events] DEBUG: http://www.foo.co.uk/events/tps-report-convention-two-the-return
2016-05-16 09:54:37 [scrapy] INFO: Closing spider (finished)
2016-05-16 09:54:37 [scrapy] INFO: Stored json feed (1 items) in: .\events.json
2016-05-16 09:54:37 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 524,
 'downloader/request_count': 2,
 'downloader/request_method_count/GET': 2,
 'downloader/response_bytes': 6187,
 'downloader/response_count': 2,
 'downloader/response_status_count/200': 2,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2016, 5, 16, 8, 54, 37, 271000),
 'item_scraped_count': 1,
 'log_count/DEBUG': 80,
 'log_count/INFO': 8,
 'response_received_count': 2,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2016, 5, 16, 8, 54, 36, 913000)}
2016-05-16 09:54:37 [scrapy] INFO: Spider closed (finished)

然后在抓取生成的 events.json 文件中,唯一似乎被抓取的页面是脚本顶部指定的起始 url,而实际上我需要所有匹配/events/的页面而是被抓取掉。

我不确定如何进行此操作,因此非常感谢对此事的任何帮助。

谢谢。

最佳答案

你应该创建一个 Item目的。也可以使用 CrawlSpider你进口的。我对您的代码做了一些更改,尝试使用它。

import scrapy
from scrapy.http import Request
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from urlparse import urljoin
#todo
from your_project.items import YourItem

class EventsSpider(CrawlSpider):
    name = "events"
    allowed_domains = ["foo.bar"]
    start_urls = [
        'http://www.foo.bar/events/',
    ]
    rules = (
        Rule(LinkExtractor(), callback='parse_item', follow=True),
    )

    def parse_item(self, response):
        item = YourItem()
        item['url'] = response.url
        item['language'] = response.xpath('//meta[@name=\'Language\']/@content').extract()
        item['description'] = response.xpath('//meta[@name=\'Description\']/@content').extract()
        yield item

        for url in response.xpath('//a/@href').extract():
            if url and not url.startswith('#'):
                self.logger.debug(urljoin(response.url, url))
                scrapy.http.Request(urljoin(response.url, url))

关于python - 带有 scrapy 的递归蜘蛛只抓取第一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37250312/

相关文章:

python - Python 是否优化尾递归?

javascript - Scrapy从javascript脚本中提取数据

python - Scrapy - 提交带有多个按钮的表单

django - 将信息从 Scrapy 爬虫持续导出到 Django 应用程序数据库的最佳方法是什么?

python - 遍历 Redis 键

python - 为什么 `gensim` 中的 tf-idf 模型在我转换语料库后丢弃了术语和计数?

python - 在Python中执行类似于Linux中的curl的URL - JenkinsAPI

python 解析lxml中的特殊字符

python - 如何让 Scrapy 使用 Python 2.7.10 而不是 3.4.3

python - 如何在python中深度复制队列