FF Xpather 到 Nokogiri——我可以复制和粘贴吗? [英] FF Xpather to Nokogiri -- Can I just copy and paste?

查看:73
本文介绍了FF Xpather 到 Nokogiri——我可以复制和粘贴吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我手动执行此操作,然后卡住了,我无法弄清楚为什么它不起作用.我下载了 xpather,它给了我:/html/body/center/table/tbody/tr[3]/td/table 作为我想要的项目的路径.我已经手动确认这是正确的,但是当我将其粘贴到我的代码中时,它所做的只是返回 nil

I was doing this manually and then I got stuck and I can't figure out why it's not working. I downloaded xpather and it is giving me: /html/body/center/table/tbody/tr[3]/td/table as the path to the item I want. I have manually confirmed that this is correct but when I paste it into my code, all it does is return nil

这是我的代码:

a = parentdoc.at_xpath("//html/body/center/table/tbody/tr[3]/td/table[1]")
puts a

如果我这样做:

a = parentdoc.at_xpath("//html/body/center")
puts a

我从页面中获得了大量文本.我可以继续添加元素,直到我点击 tbody 然后它再次返回 nil.我什至尝试过类似的东西://html/body/center/table/*/tr[3] 并且做了同样的事情返回 nil

I get a huge chunk of text from the page. I can keep adding elements until I hit tbody then it returns nil again. I even tried something like: //html/body/center/table/*/tr[3] and that did the same thing returning nil

我错过了什么?

推荐答案

您的问题是 Firefox 正在插入 HTML 中不存在的 元素.当您使用 xpather 时,它从浏览器使用的 HTML(而不是 ycombinator.com 返回的原始 HTML)开始工作,并找到以下路径:

Your problem is that Firefox is inserting a <tbody> element that is not present in the HTML. When you use xpather, it is working from the HTML that the browser is using (rather than the raw HTML that ycombinator.com is returning) and it finds this path:

//html/body/center/table/tbody/tr[3]/td/table

Nokogiri 将使用原始 HTML,所以你想要这个

Nokogiri will be working with the raw HTML so you want this

//html/body/center/table/tr[3]/td/table

当我将该 XPath 应用到您评论中的 URL 时:

When I apply that XPath to the URL in your comment:

doc.at_xpath('//html/body/center/table/tr[3]/td/table').text

我得到这个输出:

"csoghoian 1 hour ago  | link | parent2 responses:1. Chrome is the only major browser not to ...

这篇关于FF Xpather 到 Nokogiri——我可以复制和粘贴吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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