一行 nokogiri xpath 查找
当我查找 xpath 时,
b.xpath("td[@class='team']/img").each do |d|
puts d['id']
end
元素“d”是一个 html 节点。我可以引用其中的属性。
但是当我尝试在一行中查找相同的元素时,它会作为字符串返回。
c = b.xpath("td[@class='team']/img")[0]
数组中只有一个元素 I,我不需要循环遍历任何 .each
。有没有办法让这一行?
When I look up an xpath with
b.xpath("td[@class='team']/img").each do |d|
puts d['id']
end
The element "d" is a html node. I can reference attributes in it.
But when I attempt to look up the same element in one line, it gets returned as a string.
c = b.xpath("td[@class='team']/img")[0]
There is only one element I in the array, I don't need to loop through anything .each
. Is there a way to make this one line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我整理的一个测试:
正如您所期望的,结果输出是:
所以这两种方法实际上都返回 Nokogiri::XML::Element 类型的对象。
Here is a test I put together:
As you would expect, the resulting output is:
So both approaches are in fact returning an object of type Nokogiri::XML::Element.
我最终做了这样的事情:
I ended up doing something like this: