开发手册 欢迎您!
软件开发者资料库

Scrapy - 使用项目

Scrapy使用项目 - 从简单和简单的步骤学习Scrapy,从基本到高级概念,包括概述,环境,命令行工具,蜘蛛,选择器,项目,项目加载器,Shell,项目管道,Feed导出,请求和响应,链接提取器,设置,例外,创建项目,定义项目,第一个蜘蛛,抓取,提取项目,使用项目,以下链接,Scraped数据,日志记录,统计信息收集,发送电子邮件,Telnet控制台,Web服务。

描述

项目对象是Python的常规词汇.我们可以使用以下语法来访问类的属性 :

>>> item = DmozItem()>>> item['title'] = 'sample title'>>> item['title']'sample title'

将以上代码添加到以下示例中 :

import scrapyfrom tutorial.items import DmozItemclass MyprojectSpider(scrapy.Spider):   name = "project"   allowed_domains = ["dmoz.org"]      start_urls = [      "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",      "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"   ]   def parse(self, response):      for sel in response.xpath('//ul/li'):         item = DmozItem()         item['title'] = sel.xpath('a/text()').extract()         item['link'] = sel.xpath('a/@href').extract()         item['desc'] = sel.xpath('text()').extract()         yield item

上述蜘蛛的输出为 :

[scrapy] DEBUG: Scraped from <200 http://www.dmoz.org/Computers/Programming/Languages/Python/Books/>   {'desc': [u' - By David Mertz; Addison Wesley. Book in progress, full text,       ASCII format. Asks for feedback. [author website, Gnosis Software, Inc.\n],   'link': [u'http://gnosis.cx/TPiP/'],   'title': [u'Text Processing in Python']}[scrapy] DEBUG: Scraped from <200 http://www.dmoz.org/Computers/Programming/Languages/Python/Books/>   {'desc': [u' - By Sean McGrath; Prentice Hall PTR, 2000, ISBN 0130211192,       has CD-ROM. Methods to build XML applications fast, Python tutorial, DOM and       SAX, new Pyxie open source XML processing library. [Prentice Hall PTR]\n'],   'link': [u'http://www.informit.com/store/product.aspx?isbn=0130211192'],   'title': [u'XML Processing with Python']}