【发布时间】:2017-04-13 15:53:02
【问题描述】:
我正在使用Scrapy(使用SitemapSpider 蜘蛛)为www.apkmirror.com 构建一个刮板。到目前为止,以下工作:
DEBUG = True
from scrapy.spiders import SitemapSpider
from apkmirror_scraper.items import ApkmirrorScraperItem
class ApkmirrorSitemapSpider(SitemapSpider):
name = 'apkmirror-spider'
sitemap_urls = ['http://www.apkmirror.com/sitemap_index.xml']
sitemap_rules = [(r'.*-android-apk-download/$', 'parse')]
if DEBUG:
custom_settings = {'CLOSESPIDER_PAGECOUNT': 20}
def parse(self, response):
item = ApkmirrorScraperItem()
item['url'] = response.url
item['title'] = response.xpath('//h1[@title]/text()').extract_first()
item['developer'] = response.xpath('//h3[@title]/a/text()').extract_first()
return item
ApkMirrorScraperItem 在items.py 中定义如下:
class ApkmirrorScraperItem(scrapy.Item):
url = scrapy.Field()
title = scrapy.Field()
developer = scrapy.Field()
如果我使用命令从项目目录运行它,得到的 JSON 输出
scrapy crawl apkmirror-spider -o data.json
是一个 JSON 字典数组,键为 url、title 和 developer,对应的字符串为值。但是,我想修改它,使 developer 的值本身就是一个带有 name 字段的字典,这样我就可以像这样填充它:
item['developer']['name'] = response.xpath('//h3[@title]/a/text()').extract_first()
但是,如果我尝试这个,我会得到KeyErrors,如果我将developer 的Field(根据https://doc.scrapy.org/en/latest/topics/items.html#item-fields 是dict)初始化为developer = scrapy.Field(name=None)。我该怎么办?
【问题讨论】:
标签: python scrapy scrapy-spider