XPath 與 CSS (parsel)

前言

  • XPath 即爲XML路徑語言(XML Path Language)
  • 層疊樣式表(Cascading Style Sheets)是一種用來表現 HTML或XML等文件樣式的計算機語言
  • parsel 是從 Scrapy 獨立出來的解析器,可以用 xpath 或 css 提取 xml 或 html

實例

class相關

>>> 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'
# 匹配 111、222、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']
# 匹配 111、222、333、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']

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()
Out[28]: ['\n                ', '\n                ISSN: 2159-8274', '\n        ']
本文出自 walker snapshot
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章