前言
- XPath 即爲XML路徑語言(XML Path Language)
- 層疊樣式表(Cascading Style Sheets)是一種用來表現 HTML或XML等文件樣式的計算機語言
- parsel 是從 Scrapy 獨立出來的解析器,能夠用 XPath 或 CSS 提取 XML 或 HTML
實例
XPath 取字符串包含的方法
>>> from parsel import Selector
>>> htmlText = r'''
<html>
<body>
<div>
<em>Cancer Discovery</em><br>
eISSN: 2159-8290<br>
ISSN: 2159-8274<br>
</div>
</body>
</html>'''
>>> sel = Selector(htmlText, type='html')
# 包含
>>> sel.xpath('/html/body/div/text()[contains(., "eISSN")]').get()
'\n eISSN: 2159-8290'
# 不包含
>>> sel.xpath('/html/body/div/text()[not(contains(., "eISSN"))]').getall()
['\n ', '\n ISSN: 2159-8274', '\n ']
XPath 與 CSS 比對
>>> from parsel import Selector
>>> htmlText = r'''
<html>
<body>
<div class="value test">111</div>
<div class="value test ">222</div>
<div class="first value test last">333</div>
<div class="test value">444</div>
</body>
</html>'''
>>> sel = Selector(htmlText, type='html')
# 精確匹配 111
>>> sel.xpath('/html/body/div[@class="value test"]/text()').get()
'111'
>>> sel.css('div[class="value test"]::text').get()
'111'
# 匹配 1十一、22二、333
>>> sel.xpath('/html/body/div[contains(@class, "value test")]/text()').getall()
['111', '222', '333']
>>> sel.css('div[class*="value test"]::text').getall()
['111', '222', '333']
# 匹配 1十一、22二、33三、444
>>> sel.xpath('/html/body/div[contains(@class, "value") and contains(@class, "test")]/text()').getall()
['111', '222', '333', '444']
>>> sel.css('div.value.test::text').getall()
['111', '222', '333', '444']
本文出自
walker snapshot