- 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']}
廣告