[英]Scrapy CrawlSpider Post-processing: Finding an Average
假设我有一个类似于此示例的爬网蜘蛛:从scrapy.contrib.spiders导入CrawlSpider,从scrapy.contrib.linkextractors.sgml导入Rule,从scrapy.selector导入SgmlLinkExtractor,从scrapy.item导入HtmlXPathSelector
class MySpider(CrawlSpider):
name = 'example.com'
allowed_domains = ['example.com']
start_urls = ['http://www.example.com']
rules = (
# Extract links matching 'category.php' (but not matching 'subsection.php')
# and follow links from them (since no callback means follow=True by default).
Rule(SgmlLinkExtractor(allow=('category\.php', ), deny=('subsection\.php', ))),
# Extract links matching 'item.php' and parse them with the spider's method parse_item
Rule(SgmlLinkExtractor(allow=('item\.php', )), callback='parse_item'),
)
def parse_item(self, response):
self.log('Hi, this is an item page! %s' % response.url)
hxs = HtmlXPathSelector(response)
item = Item()
item['id'] = hxs.select('//td[@id="item_id"]/text()').re(r'ID: (\d+)')
item['name'] = hxs.select('//td[@id="item_name"]/text()').extract()
item['description'] = hxs.select('//td[@id="item_description"]/text()').extract()
return item
假设我想获取一些信息,例如每个页面的ID之和,或所有已解析页面的描述中的平均字符数。 我该怎么办?
另外,如何获得特定类别的平均值?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.