【问题标题】:How to get text from 'td' tags from 'table' tag on html page using Mechanize如何使用 Mechanize 从 html 页面上的“table”标签中的“td”标签获取文本
【发布时间】:2012-04-03 19:47:28
【问题描述】:

如何使用 Mechanize gem 从 html 页面上的 'table' 中获取 'td' 标签中的文本?

【问题讨论】:

标签: ruby mechanize


【解决方案1】:

我几乎总是将 mechanize 与 nokogiri 一起使用。这个guide 帮助我开始了。

这样的东西应该可以工作(未经测试):

require 'mechanize'
require 'nokogiri'

agent = Mechanize.new
page = agent.get("http://www.google.com/")
doc = Nokogiri::HTML(page.body, "UTF-8")
doc.xpath('//td').each do |node|
  puts node.text
end

更多关于 nokogiri 的信息here

【讨论】:

  • 谢谢,Mechanize 已经使用 nokogiri,所以使用 Mechanize 的 xpath 查找会更正确
  • 谢谢,帮我省了一些钱。过去,我总是单独要求它。
猜你喜欢
  • 1970-01-01
  • 2022-12-29
  • 1970-01-01
  • 1970-01-01
  • 2013-11-29
  • 2012-07-20
  • 2013-03-18
  • 1970-01-01
  • 2017-06-11
相关资源
最近更新 更多