XPath的幾個常用語法與例項(Python)

2021-10-02 13:39:38 字數 1477 閱讀 6320

一、xpath的語法

表示式描述

/從根節點擊取

//從匹配選擇的當前節點下選擇文件中的節點,不用考慮它們的位置

@選取屬性

.選取當前節點

. .選取當前節點的父節點

二、例項

從豆瓣電影排行榜網頁源**中(擷取了以下**用於演示。

="indent"

>

>

="ul first"

>

<

/p>

"100%"

class=""

>

="item"

>

"100" valign=

"top"

>

="nbg" href=

"" title=

"囧媽"

>

"" width=

"75" alt=

"囧媽"

class=""

/>

<

/a>

<

/td>

"top"

>

="pl2"

>

""class=""

>囧媽/

"font-size:13px;"

>俄囧 / lost in russia<

/span>

<

/a>..

....

路徑表示式為:

url_list = html.xpath(

"//div[@class='indent']/div/table//div[@class='pl2']/a/@href"

)① //div[@class=『indent』]:首先從根節點下開始選取html中class名為『indent』的div結點

② //div[@class=『indent』]/div/table:再從選擇的當前節點作為根節點,繼續往下選擇結點

③ //div[@class=『indent』]/div/table//div[@class=『pl2』]: 從匹配選擇的當前節點下選擇文件中的class名為pl2的div節點

④//div[@class=『indent』]/div/table//div[@class=『pl2』]/a:選擇到url位址所在的這個結點

⑤//div[@class=『indent』]/div/table//div[@class=『pl2』]/a/@href:規定我們要的屬性,即href.

路徑表示式為:
img_list = html.xpath(

"//div[@class='indent']/div/table//a[@class='nbg']/img/@src"

)

xpath常用語句

代表匹配所有元素節點 匹配所有屬性節點 node 匹配任何型別的節點 31 根目錄 任意位置查詢我們想要提取到 xpath常用語句 這句話,利用 從根目錄進行查詢為 html body div class head h1 text 如果使用相對路徑查詢 div class head h1 2 相同標...

mysql常用語法 MySQL常用語法

更新資料 update 表名 set 健名 更改的值 where 建 刪除資料 delete from 表名 where 索引 create index 索引名稱 on 表名 鍵名 建立試圖 create view 檢視名 鍵,鍵 as select matchno,abs won lost fro...

mysql基本常用語法 mysql 常用語法

1.1.開啟 service mysql start 1.2.關閉 service mysql stop 1.3.重啟 service mysql restart 2.1.密碼明文 語法 mysql u使用者名稱 p使用者密碼 舉例 mysql uroot p123456 2.2.密碼密文 語法 m...