我需要一个Xpath表达式来选择以某个值开头的值.对于此实例,我使用id字段.
@id=[starts-with(name(),'value')
Run Code Online (Sandbox Code Playgroud)
以下不起作用.有没有办法使用带有标签之间值的starts-with命令?或者在xpath中有另一种选择匹配具有已知值的值的方法.
这是我想要深入研究的xml示例:
<bean>
<id>AnnotationsBasedJMXAutoExporter</id>
<class>org.springframework.jmx.export.MBeanExporter</class>
<lazy-init>false</lazy-init>
<property>assembler
<!-- will create management interface using annotation metadata -->
<bean>
Run Code Online (Sandbox Code Playgroud) 我有一个节点如下:
<span class="portal-text-medium">Office Hours</span>
Run Code Online (Sandbox Code Playgroud)
对于我使用的XPath
//span[text()='Office Hours']
Run Code Online (Sandbox Code Playgroud)
这应该工作,但它永远不会.我可以使用*contains(text(),'Office Hours')]*但是找不到完全匹配,我必须验证没有"*".这不是它唯一不适合我的时间.我之前看到它有用,所以我不知道出了什么问题.任何的想法?
是的,我可以,并且确实使用,starts-with但它并不完全相同.
我遇到了 xpath 表达式的问题。请有人帮助我吗?
所以我的 xpath 是 -".//td[starts-with(text(), 'IMT - Office Admin:')]"
DOM:
<td>
<input type="checkbox" name="partyEditF:j_id698:6:j_id700">
IMT - Office Admin: Ability to edit everything within your office including the office's information and listings
</td>
Run Code Online (Sandbox Code Playgroud) <a href="javascript:void(0)" title="home">
<span class="menu_icon">Maybe more text here</span>
Home
</a>
Run Code Online (Sandbox Code Playgroud)
因此,对于上面的代码,当我编写//a为XPath时,它会突出显示,但是当我编写时//a[contains(text(), 'Home')],它不会被突出显示。我认为这很简单,应该有效。
我的错在哪里
假设我有:
<A>
<B>C</B>
<D>E</D>
</A>
Run Code Online (Sandbox Code Playgroud)
然后我可以输出B元素(包括标签):
//B
Run Code Online (Sandbox Code Playgroud)
哪个会回归
<B>C</B>
Run Code Online (Sandbox Code Playgroud)
但是为什么谓词中不需要text()? 以下两行给出相同的输出:
/A[B = 'C']/D
/A[B/text() = 'C']/D
Run Code Online (Sandbox Code Playgroud)
如果XPATH是干净的构造我会期望它(或在某种其他元素结构):
/A[B = <B>C></B>]/D
Run Code Online (Sandbox Code Playgroud)
和:
/A[B/text()='C']/D
Run Code Online (Sandbox Code Playgroud)
有人可以给我一个理由,为什么输出需要text(),但谓词不需要它?
假设我有一段这样的 HTML:
<a>Ask Question<other/>more text</a>
Run Code Online (Sandbox Code Playgroud)
我可以匹配这段XPath:
//a[text() = 'Ask Question']
Run Code Online (Sandbox Code Playgroud)
或者...
//a[text() = 'more text']
Run Code Online (Sandbox Code Playgroud)
或者我可以使用点来匹配整个内容:
//a[. = 'Ask Questionmore text']
Run Code Online (Sandbox Code Playgroud)
这篇文章.描述了(dot) 和 之间的区别text(),但简而言之,第一个返回单个元素,而后者返回一个元素列表。但这对我来说有点奇怪。因为 whiletext()可以用来匹配列表中的任意一个元素,但对于 XPath 函数来说,情况并非如此contains()。如果我这样做:
//a[contains(text(), 'Ask Question')]
Run Code Online (Sandbox Code Playgroud)
...我收到以下错误:
错误: contains() 的第一个参数所需的基数为一或零
为什么它text()在使用完整匹配(等于)时有效,但在部分匹配(包含)时不起作用?
我正在使用lxml进行 HTML 屏幕抓取,并且需要通过 选择一个元素text(),其方式类似于使用纯 XML 在另一个问题上所做的操作,但是无论发生什么情况,我都会收到无效谓词错误。我将其简化为以下示例:
import lxml.html
sample_html = "<div><h2>test string</h2><h2>other string</h2></div>"
sample_tree = lxml.html.fromstring(sample_html)
sample_tree.findall('.//h2[text()="test string"]')
Run Code Online (Sandbox Code Playgroud)
虽然这应该是有效的,但我不断收到错误:
File "<string>", line unknown
SyntaxError: invalid predicate
Run Code Online (Sandbox Code Playgroud)
text()有关如何在解析 HTML 时正确让 lxml 选择元素的任何提示吗?