【发布时间】:2014-07-19 04:47:04
【问题描述】:
我的目标是获得“我想要这个文本”文本。
<div class="class1">
<b>xxxxx</b>
<a href="http://example.com/want/xxxx">I want this text</a>
<a href="http://example.com/want/yyyy">I want this text</a>
<b>yyyyy</b>
<a href="http://example.com/unnece/xxxx">no need</a>
<a href="http://example.com/unnece/xxxx">no need</a>
</div>
<div class="class2">
<a href="http://example.com/want/xxxx">no need</a>
<a href="http://example.com/want/yyyy">no need</a>
</div>
我尝试编写代码。
test = agent.page.search("div[@class='class1'] / a").text
但是,我也得到了“不需要”的文字
我该怎么做?我正在使用宝石:
require 'mechanize'
require 'open-uri'
如果可能的话,我想在文本之间放置逗号...
【问题讨论】:
-
您可以尝试使用 gem 'nokogiri',它有 css 选择器和查找许多元素的方法
-
@Pavel - Mechanize 是 Nokogiri 的姊妹项目,并将其作为解析器包含在内
标签: ruby-on-rails ruby web-scraping mechanize