1. 程式人生 > 其它 >python解析頁面DOM樹形成xpath列表,並計算DOM樹的最大深度

python解析頁面DOM樹形成xpath列表,並計算DOM樹的最大深度

## 參考

[(14條訊息) python解析頁面DOM樹形成xpath列表,並計算DOM樹的最大深度_Together_CZ的部落格-CSDN部落格](https://blog.csdn.net/Together_CZ/article/details/73718463 )]

[[Solved] Python how to get the full contents of a node using xpath & lxml? - Code Redirect](https://coderedirect.com/questions/403636/how-to-get-the-full-contents-of-a-node-using-xpath-lxml )]

[python3解析庫lxml - Py.qi - 部落格園](https://www.cnblogs.com/zhangxinqi/p/9210211.html )]

You can usegetpath()to get xpath fromelement, for example :

import requests
from lxml import html

page = requests.get("http://www.w3schools.com/xpath/")
root = html.fromstring(page.text)
tree = root.getroottree()
result = root.xpath('//*[. = "XML"]')
for r in result:
    print(tree.getpath(r))

Output :

/html/body/div[3]/div/ul/li[10]
/html/body/div[3]/div/ul/li[10]/a
/html/body/div[4]/div/div[2]/div[2]/div[1]/div/ul/li[2]
/html/body/div[5]/div/div[6]/h3
/html/body/div[6]/div/div[4]/h3
/html/body/div[7]/div/div[4]/h3