我有这样的代码:只能通过XPath获得特定属性的值吗?
doc = Nokogiri::HTML("<a href='foo.html'>foo</a><a href='bar.html'>bar</a>")
doc.xpath('//a/@href').map(&:value) # => ["foo.html", "bar.html"]
它可以作为我的预期。
但是出于好奇心,我想知道,只有使用XPath才能获得href
属性的值吗?
我有这样的代码:只能通过XPath获得特定属性的值吗?
doc = Nokogiri::HTML("<a href='foo.html'>foo</a><a href='bar.html'>bar</a>")
doc.xpath('//a/@href').map(&:value) # => ["foo.html", "bar.html"]
它可以作为我的预期。
但是出于好奇心,我想知道,只有使用XPath才能获得href
属性的值吗?
找到属性第一
例如: 站点名称: https://www.easymobilerecharge.com/
我们要找到 “MTS” 链接
在你的情况下,找到这个元素,我们可以使用的x路例如: // a [contains(text(),'MTS')]
现在要获取href属性,请使用: // a [contains(text(),'MTS')]/@ href
从this question的第一个答案来看,答案似乎是肯定否。它提供了
xml.xpath("//Placement").attr("messageId")
这是非常接近“只有XPath”,但不完全。由你来判断这对你是否足够。
你是什么意思'只有使用XPath'?这个'// a/@ href'是'XPath'来获得'href'属性值。 http://videlibri.sourceforge.net/cgi-bin/xidelcgi?&data=%3Ca%20href%3D%27foo.html%27%3Efoo%3C%2Fa%3E%3Ca%20href%3D%27bar.html%27 %3Ebar%3C%2Fa%3E&=&extract =%2F%2Fa%2F%40href&=&input-format = auto&printed-node-format = text&output-format = adhoc&compatibility = Enable%20all%20extensions&dot-notation = unambiguous&extract-kind = xpath3 – Andersson
我想也许有一个xpath类似'// a/@ href/value()'。似乎没有。谢谢。 – ironsand