我写了一些代码来返回和显示 this website 上最近的五个帖子.然而,当我使用 for
运行代码时循环,返回一个空字符串。代码如下:
require 'rubygems'
require 'mechanize'
agent = Mechanize.new
site = "http://metarand.com"
page = agent.get(site)
for i in 1..5
puts "#{i}) - #{page.search("#boxes :nth-child(i) .clearfix .blog-title")}"
end
代码有什么问题,我该如何解决?
最佳答案
我相信,你需要这样的东西:
(1..5).each {|i| puts %Q~#{i} - #{page.at("#boxes :nth-child(#{i}) .clearfix .blog-title").text}~ }
关于ruby - 网页抓取返回空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14029444/