将来自referer的Response对象引入parse_item回调

时间:2014-01-29 18:05:08

标签: python python-2.7 screen-scraping scrapy web-crawler

问题

我正在尝试抓取像YouTube这样的网站,其中包含一系列视频列表以及指向各个视频的链接。我想要做的是在使用parse_item()进入特定视频之前抓取视频的缩略图。

问题是我不知道如何将“列表视图”的Response对象引入parse_item()函数。我知道您可以使用process_request拦截请求并向Request对象插入元数据,但我无法弄清楚如何获取列表视图Response。

这个问题有不同的方法吗?

我的代码:

import re
import datetime

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.selector import Selector
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor

from ..items import ExampleItem


class ExampleSpider(CrawlSpider):
    """
    Crawler for: www.example.com
    """

    name = "example"

    allowed_domains = ['www.example.com']

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

    rules = (
        Rule(SgmlLinkExtractor(
            restrict_xpaths=["//div[@class='pagination']"]
        )),

        Rule(SgmlLinkExtractor(
            restrict_xpaths=["//ul[@class='list']"],
            deny=['/user/'],
        ), callback='parse_item', process_request='parent_url')
    )

    def parent_url(self, request):

        request.meta['parent_page'] = '' #  Get the parent response somehow?
        return request

    def parse_item(self, response):

        sel = Selector(response)

        item = ExampleItem()

        duration = sel.css('.video span::text')[0].extract()

        item['title'] = sel.css('.title::text')[0].extract()
        item['description'] = sel.xpath('//div[@class="description"]/text()').extract()
        item['duration'] = self._parse_duration(duration)
        item['link'] = response.url

        return item

    def _parse_duration(self, string):
        """
        Parse the duration field for times
        return Datetime object
        """

        if len(string) > 20:
            return datetime.datetime.strptime(string, '%H hours %M min %S sec').time()

        if '60 min' in string:
            string.replace('60 min', '01 hours 00 min')
            return datetime.datetime.strptime(string, '%H hours %M min %S sec')

        return datetime.datetime.strptime(string, '%M min %S sec').time()

1 个答案:

答案 0 :(得分:3)

我假设您想知道从中提取链接(请求)的URL。

您可以覆盖方法_requests_to_follow以传递请求的源页面:

    def _requests_to_follow(self, response):
        for req in super(ExampleSpider, self)._requests_to_follow(response):
            req.meta['parent_page'] = response.url
            yield req