【发布时间】:2014-02-21 17:27:04
【问题描述】:
问题
我正在尝试抓取像 YouTube 这样的网站,该网站有一个包含大量视频的列表以及指向单个视频的链接。我要做的是在使用 parse_item() 进入特定视频之前抓取视频的缩略图。
问题是我不知道如何将“列表视图”的响应对象带入 parse_item() 函数。我知道您可以使用 process_request 拦截请求并将元数据插入 Request 对象,但我不知道如何获取列表视图响应。
这个问题有不同的方法吗?
我的代码:
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()
【问题讨论】:
标签: python python-2.7 screen-scraping scrapy web-crawler