使用 Ruby/Mechanize(和 Nokogiri)从 HTML 中提取单个字符串
我正在从论坛中提取数据。我的脚本基于工作正常。现在我需要从单个帖子中提取日期和时间(2009 年 12 月 21 日,20:39)。我无法让它发挥作用。我使用 FireXPath 来确定 xpath。
示例代码:
require 'rubygems'
require 'mechanize'
post_agent = WWW::Mechanize.new
post_page = post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')
puts post_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div[2]/text()').to_s.strip
puts post_page.parser.at_xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div[2]/text()').to_s.strip
puts post_page.parser.xpath('//[@id="post1960370"]/tbody/tr[1]/td/div[2]/text()')
我的所有尝试都以空字符串或错误结束。
我找不到任何有关在 Mechanize 中使用 Nokogiri 的文档。 Mechanize 文档在页面底部说道:
使用 Mechanize 导航到需要抓取的页面后,然后使用 Nokogiri 方法抓取它。
但有什么方法呢?我在哪里可以阅读有关它们的示例和解释的语法?我在 Nokogiri 网站 上也没有找到任何内容。
I am extracting data from a forum. My script based on is working fine. Now I need to extract date and time (21 Dec 2009, 20:39) from single post. I cannot get it work. I used FireXPath to determine the xpath.
Sample code:
require 'rubygems'
require 'mechanize'
post_agent = WWW::Mechanize.new
post_page = post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')
puts post_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div[2]/text()').to_s.strip
puts post_page.parser.at_xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div[2]/text()').to_s.strip
puts post_page.parser.xpath('//[@id="post1960370"]/tbody/tr[1]/td/div[2]/text()')
all my attempts end with empty string or an error.
I cannot find any documentation on using Nokogiri within Mechanize. The Mechanize documentation says at the bottom of the page:
After you have used Mechanize to navigate to the page that you need to scrape, then scrape it using Nokogiri methods.
But what methods? Where can I read about them with samples and explained syntax? I did not find anything on Nokogiri's site either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
拉德克.我将向您展示如何钓鱼。
当您调用
Mechanize::Page::parser
时,它会为您提供 Nokogiri 文档。因此,您的“xpath
”和“at_xpath
”调用正在调用 Nokogiri。问题出在你的 xpath 上。一般来说,从您可以使用的最通用的 xpath 开始,然后缩小范围。因此,例如,不要这样:从这样开始:
这会获取任何地方的任何表,然后将它们打印为 html。检查 HTML,看看它带回了哪些表。当您只想要一张桌子时,它可能会抓取几张桌子,因此您需要告诉它如何挑选出您想要的一张桌子。例如,如果您注意到所需的表具有 CSS 类“
userdata
”,那么请尝试以下操作:任何时候您没有取回数组,您就搞砸了 xpath,因此请修复它在继续之前。一旦你得到了你想要的表格,然后尝试获取行:
如果有效,那么去掉“
to_html
”,你现在就有了一个 Nokogiri 节点数组,每个节点一个表格行。这就是你要做的。
Radek. I'm going to show you how to fish.
When you call
Mechanize::Page::parser
, it's giving you the Nokogiri document. So your "xpath
" and "at_xpath
" calls are invoking Nokogiri. The problem is in your xpaths. In general, start out with the most general xpath you can get to work, and then narrow it down. So, for example, instead of this:start with this:
This gets the any tables, anywhere, and then prints them as html. Examine the HTML, to see what tables it brought back. It probably grabbed several when you want only one, so you'll need to tell it how to pick out the one table you want. If, for example, you notice that the table you want has CSS class "
userdata
", then try this:Any time you don't get back an array, you goofed up the xpath, so fix it before proceding. Once you're getting the table you want, then try to get the rows:
If that worked, then take off the "
to_html
" and you now have an array of Nokogiri nodes, each one a table row.And that's how you do it.
我认为您已经从 Firebug 复制了此内容,Firebug 为您提供了一个额外的 tbody,但实际代码中可能不存在...所以我的建议是删除该 tbody 并重试。
如果它仍然不起作用......那么遵循韦恩·康拉德的流程是最好的!
I think you have copied this from Firebug, firebug gives you an extra tbody, which might not be there in actual code... so my suggestion is to remove that tbody and try again.
if it still doesn't work ... then follow Wayne Conrad's process that's the best!