我正在使用Mechanize gem。如何在两个div标签之间获取内容?
"<div class='a'></div>content<div class='a'></div>"
问题是,内容介于<p>
标记之间。
<div>
<div class='a'>Content1</div>
<p></p>
<p></p>
<p></p>
<p></p>
<div class='a'>Content2</div>
<p></p>
<p></p>
<p></p>
<p></p>
</div>
答案 0 :(得分:-1)
您在检索页面后使用Nokogiri
来解析页面:
m = Mechanize.new
result = m.get("http://google.com")
html = Nokogiri::HTML(result.body)
divs = html.xpath('//div').map { |div| div.content } # here you can do whatever is needed with the divs
# I've mapped their content into an array