- Scrapy 教程
- Scrapy - 首頁
- Scrapy 基本概念
- Scrapy - 概述
- Scrapy - 環境配置
- Scrapy - 命令列工具
- Scrapy - 爬蟲 (Spider)
- Scrapy - 選擇器
- Scrapy - 資料項 (Item)
- Scrapy - 資料項載入器 (Item Loader)
- Scrapy - Shell
- Scrapy - 資料項管道 (Item Pipeline)
- Scrapy - 資料匯出
- Scrapy - 請求 & 響應
- Scrapy - 連結提取器
- Scrapy - 設定
- Scrapy - 異常處理
- Scrapy 實戰專案
- Scrapy - 建立專案
- Scrapy - 定義資料項
- Scrapy - 第一個爬蟲
- Scrapy - 爬取資料
- Scrapy - 資料提取
- Scrapy - 使用資料項
- Scrapy - 跟蹤連結
- Scrapy - 爬取的資料
- Scrapy 有用資源
- Scrapy - 快速指南
- Scrapy - 有用資源
- Scrapy - 討論
Scrapy - 資料提取
描述
為了從網頁中提取資料,Scrapy 使用基於XPath和CSS表示式的選擇器技術。以下是一些XPath表示式的示例:
/html/head/title − 這將選擇HTML文件中<head>元素內的<title>元素。
/html/head/title/text() − 這將選擇相同<title>元素中的文字。
//td − 這將選擇所有<td>元素。
//div[@class = "slice"] − 這將選擇所有包含屬性class = "slice"的div元素。
選擇器有四種基本方法,如下表所示:
| 序號 | 方法 & 描述 |
|---|---|
| 1 | extract() 它返回一個包含所選資料的Unicode字串。 |
| 2 | re() 它返回一個Unicode字串列表,這些字串是在使用正則表示式作為引數提取時獲得的。 |
| 3 | xpath() 它返回一個選擇器列表,表示由作為引數提供的xpath表示式選擇的節點。 |
| 4 | css() 它返回一個選擇器列表,表示由作為引數提供的CSS表示式選擇的節點。 |
在Shell中使用選擇器
為了使用內建的Scrapy shell演示選擇器,你需要在你的系統中安裝IPython。這裡需要注意的是,在執行Scrapy時,URL應該包含在引號中;否則包含“&”字元的URL將無法工作。你可以使用專案頂層目錄中的以下命令啟動shell:
scrapy shell "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/"
shell介面如下所示:
[ ... Scrapy log here ... ]
2014-01-23 17:11:42-0400 [scrapy] DEBUG: Crawled (200)
<GET http://www.dmoz.org/Computers/Programming/Languages/Python/Books/>(referer: None)
[s] Available Scrapy objects:
[s] crawler <scrapy.crawler.Crawler object at 0x3636b50>
[s] item {}
[s] request <GET http://www.dmoz.org/Computers/Programming/Languages/Python/Books/>
[s] response <200 http://www.dmoz.org/Computers/Programming/Languages/Python/Books/>
[s] settings <scrapy.settings.Settings object at 0x3fadc50>
[s] spider <Spider 'default' at 0x3cebf50>
[s] Useful shortcuts:
[s] shelp() Shell help (print this help)
[s] fetch(req_or_url) Fetch request (or URL) and update local objects
[s] view(response) View response in a browser
In [1]:
當shell載入後,你可以分別使用response.body和response.header訪問主體或頭部。類似地,你可以使用response.selector.xpath()或response.selector.css()對響應執行查詢。
例如:
In [1]: response.xpath('//title')
Out[1]: [<Selector xpath = '//title' data = u'<title>My Book - Scrapy'>]
In [2]: response.xpath('//title').extract()
Out[2]: [u'<title>My Book - Scrapy: Index: Chapters</title>']
In [3]: response.xpath('//title/text()')
Out[3]: [<Selector xpath = '//title/text()' data = u'My Book - Scrapy: Index:'>]
In [4]: response.xpath('//title/text()').extract()
Out[4]: [u'My Book - Scrapy: Index: Chapters']
In [5]: response.xpath('//title/text()').re('(\w+):')
Out[5]: [u'Scrapy', u'Index', u'Chapters']
提取資料
要從普通的HTML網站提取資料,我們必須檢查網站的原始碼以獲取XPaths。檢查後,您可以看到資料將位於ul標籤中。選擇li標籤中的元素。
以下程式碼行顯示了不同型別資料的提取:
選擇li標籤中的資料:
response.xpath('//ul/li')
選擇描述:
response.xpath('//ul/li/text()').extract()
選擇網站標題:
response.xpath('//ul/li/a/text()').extract()
選擇網站連結:
response.xpath('//ul/li/a/@href').extract()
以下程式碼演示了上述提取器的用法:
import scrapy
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'):
title = sel.xpath('a/text()').extract()
link = sel.xpath('a/@href').extract()
desc = sel.xpath('text()').extract()
print title, link, desc