网站更新内容:请访问: https://bigdata.ministep.cn/

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 use getpath() to get xpath from element, 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
 
 

 

posted @ 2021-10-26 19:58  ministep88  阅读(229)  评论(0编辑  收藏  举报
网站更新内容:请访问:https://bigdata.ministep.cn/