python-查找包含字符串的元素的xpath [英] python - find xpath of element containing string

查看:202
本文介绍了python-查找包含字符串的元素的xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个小脚本,该脚本应该在页面中找到一些特定的字符串并返回包含该字符串的元素的xpath. 目的是使用此xpath查找具有相同上下文的字符串.

I build a small script that supposed to find some specific string in a page and return the xpath of the element containing this string. The purpose is to use this xpath for finding string with same context.

我正在使用以下代码:

import requests
from lxml import html
page = requests.get("http://www.w3schools.com/xpath/")
tree = html.fromstring(page.text)
result = tree.xpath('//*[. = "XML"]')

result[0]返回<Element b at 0x7f034a08e940>,但我仍然不知道如何找到该元素的XPath.

result[0] returns <Element b at 0x7f034a08e940> and I can't figure out how to find this element's XPath anyway .

我想要的字符串是:

/html/body/div[4]/div/div[2]/div[2]/div[1]/div/ul/li[2]

推荐答案

您可以使用

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))

输出:

/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

这篇关于python-查找包含字符串的元素的xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆