scrapy 2.3 HTML響應(yīng)的選擇器示例

2021-06-04 10:53 更新

這里有一些 ?Selector? 舉例說明幾個(gè)概念。在所有情況下,我們假設(shè) ?Selector? 用一個(gè) ?HtmlResponse? 這樣的對(duì)象:

sel = Selector(html_response)

1、選擇全部 ?<h1>? 來自HTML響應(yīng)正文的元素,返回 ?Selector? 對(duì)象(即 ?SelectorList? 對(duì)象):

sel.xpath("http://h1")

2、提取所有文本 <h1> 元素,返回字符串列表:

sel.xpath("http://h1").getall() # this includes the h1 tag
sel.xpath("http://h1/text()").getall() # this excludes the h1 tag

3、全部迭代 <p> 標(biāo)記并打印其類屬性:

for node in sel.xpath("http://p"): 
print(node.attrib['class'])
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)