- Scrapy 教程
- Scrapy - 主页
- Scrapy 基本概念
- Scrapy - 概述
- Scrapy - 环境
- Scrapy - 命令行工具
- Scrapy - 爬虫
- Scrapy - 选择器
- Scrapy - 项
- Scrapy - 项加载器
- Scrapy - Shell
- Scrapy - 项管道
- Scrapy - Feed 导出
- Scrapy - 请求和响应
- Scrapy - 链接提取器
- Scrapy - 设置
- Scrapy - 异常
- Scrapy Live 项目
- Scrapy - 创建项目
- Scrapy - 定义一项
- Scrapy - 首个爬虫
- Scrapy - 爬行
- Scrapy - 提取项
- Scrapy - 项的使用
- Scrapy - 关注链接
- Scrapy - 爬取的数据
- Scrapy 的有用资源
- Scrapy - 快速指南
- Scrapy - 有用资源
- Scrapy - 讨论
Scrapy - 项的使用
描述
Item 对象是 Python 的普通词典。我们可以使用以下语法访问类的属性 −
>>> item = DmozItem() >>> item['title'] = 'sample title' >>> item['title'] 'sample title'
将上述代码添加到以下示例中 −
import scrapy from tutorial.items import DmozItem class 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']}
广告