- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如何遍历到找到的某个元素,然后继续找到下一个元素?在我的示例中,我尝试搜索第一个元素,获取文本,然后继续搜索,直到找到下一个标签或找到特定标签。我还需要考虑标签的原因是因为我想在那里做一些事情。
HTML
<table border=0>
<tr>
<td width=180>
<font size=+1><b>apple</b></font>
</td>
<td>Description of an apple</td>
</tr>
<tr>
<td width=180>
<font size=+1><b>banana</b></font>
</td>
<td>Description of a banana</td>
</tr>
<tr>
<td><img vspace=4 hspace=0 src="common/dot_clear.gif"></td>
</tr>
...Then this repeats itself in a similar format
当前scrape.rb
#...
document.at_css("body").traverse do |node|
#if <font> is found
#puts text in font
#else if <img> is found then
#puts img src and continue loop until end of document
end
谢谢!
最佳答案
很有趣。您基本上想要遍历树中的所有 children 并根据获得的节点执行一些操作。
所以我们可以这样做:
#Acquiring dummy page
page = Nokogiri::HTML(open('http://en.wikipedia.org/wiki/Ruby_%28programming_language%29'))
现在,如果您想开始遍历所有 body
元素,我们可以使用 XPath
来拯救我们。 XPath 表达式://body//*
将返回 body
中的所有 children 和 grand-children。
这将返回类为 Nokogiri::XML::Element
的元素数组
page.xpath('//body//*')
page.xpath('//body//*').first.node_name
#=> "div"
因此,您现在可以遍历该数组并执行您的操作:
page.xpath('//body//*').each do |node|
case node.name
when 'div' then #do this
when 'font' then #do that
end
end
关于ruby - 您如何使用 Nokogiri 遍历 HTML 文档、搜索并跳到下一个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20766854/
我在 OSX 上使用 RVM 和之前的 RBENV。我在 ./gems 中相对于我的项目路径安装了我的 gems 我正在使用 aws-sdk 并尝试使用 s3 客户端导致 nokogiri/nokog
我正在尝试使用以下版本在 Mac OS Big Sur 上构建 Rails 应用程序...... $ rails --version Rails 5.0.7.2 $ ruby --version ru
我正在尝试抓取 http://www.ign.com/games/reviews使用 Nokogiri,我想实例化与页面上每个游戏评论相对应的新评论对象。当然,我还想从每条评论中获取每个数字分数,并将
我正在编写 Lynda 的 Ruby on Rails 教程,一切都按预期进行。所有安装和一切都运行完美。 但是创建项目后,当我尝试启动服务器(cmd:rails server)时,我收到错误消息:
尝试按如下方式在生产模式下运行 rake Assets 预编译。 rake assets:precompile 它在 ubuntu 14.04(32 位)和 16.06(32 位)上运行良好。但是在
我需要一种方法来运行 nokogiri 脚本 #parser.rb require 'nokogiri' def parseit() //... end 并在 jruby 的 main.rb 下面运行
在解析缩进的 XML 时,不重要的空白文本节点是从结束标记和开始标记之间的空白创建的。例如,来自以下 XML: Tove Jani Reminder Don't forget me
Nokogiri 在我的 gem 文件中。 bundle 安装返回预期的输出: Using nokogiri (1.4.4) Your bundle is complete! Use `bundle
我安装了 Ruby 和 Nokogiri gem。在 irb 中,我尝试: require 'nokogiri' require 'open-uri' url = 'http://www.amazon
我想使用 XSL 将 XML 文档转换为 HTML,稍微修改一下,然后将其呈现出来。这基本上就是我正在做的: source = Nokogiri::XML(File.read 'source.xml'
我的 Ruby on Rails 应用程序使用 Rails 2.2.2 和 Ruby 1.8.7。 尝试运行时 RAILS_ENV=production rake gems:install 或任何佣金
在 IRB 中,当我键入 require“nokogiri”时,出现以下错误: LoadError: cannot load such file -- nokogiri 我的 gemfile 中安装了
我正在使用 DevKit 在 Windows 8.1 上运行全新安装的 Ruby 2.2.1。安装后我运行: gem install rails rails new testapp cd testap
我正在尝试安装 nokogiri,因为它是启动 rails 所必需的 $ rails s /usr/local/rvm/gems/ruby-1.9.3-p194@global/gems/bun
我终于设法解析了网站的部分内容: get '/' do url = '' data = Nokogiri::HTML(open(url)) @rows = data.css("td[val
我第一次使用 Nokogiri 搜索 HTML 文档。当我创建一个变量(并打印)时,它等于: beteween Nokogiri::HTML(open(url).read) 它似乎输出与完全相同的东西
如果我尝试安装 nokogiri,我会收到以下错误: rvmsudo gem install nokogiri ERROR: Error installing nokogiri: nokog
我在屏幕抓取 rake 任务中有以下代码 page = agent.get("https://domainname.co.uk/unit/27/logs?type=incoming&page=8")
我尝试(出于测试目的)解析 Google 商家 XML 提要,定义为: EasyOptic 2014-08-01T16:31:
如何将“foo”替换为“bar”? 来自 foo1foo2foo4foo5foo6 至 bar1bar2bar4bar5bar6 我只想替换标签内部内容,没有标签属性。 有什么想法吗? 最佳答案 re
我是一名优秀的程序员,十分优秀!