FF Xpather to Nokogiri — Can I just copy and paste?

你离开我真会死。 提交于 2019-12-12 04:39:46

问题


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

Here is my code:

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

If I do something like this:

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

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

What am I missing?


回答1:


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 will be working with the raw HTML so you want this

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

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

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

I get this output:

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


来源:https://stackoverflow.com/questions/7540506/ff-xpather-to-nokogiri-can-i-just-copy-and-paste

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!