[英]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.