【发布时间】:2017-10-28 11:46:16
【问题描述】:
我对编程很陌生,需要一些关于我的代码的帮助/反馈。 我的目标是抓取运行良好的数据,然后在编号列表中向我的用户显示该数据。我只是很难显示这些数据。我没有收到任何错误,我的程序完全跳过了我的方法。提前感谢您的任何帮助/反馈!
class BestPlaces::Places
attr_accessor :name, :population, :places
@@places = []
def self.list_places
# puts "this is inside list places"
self.scrape_places
end
def self.scrape_places
doc = Nokogiri::HTML(open("https://nomadlist.com/best-cities-to-live"))
places = doc.search("div.text h2.itemName").text
rank = doc.search("div.rank").text
places.collect{|e| e.text.strip}
puts "you are now in title"
@@places << self.scrape_places
puts "#{rank}. #{places}"
end
end
end
CLI Page:
class BestPlaces::CLI
def list_places
puts "Welcome to the best places on Earth!"
puts @places = BestPlaces::Places.list_places
end
def call
list_places
menu
goodbye
end
end
【问题讨论】:
-
你如何运行你的代码?
-
本地,在我的 bin 文件夹中只有一个 CLI 程序
-
您必须调用您的方法,即
BestPlaces::Places.list_places(我认为这是您的入口点)。把它放在文件的底部。看起来你在@@places << self.scape_places行有无限循环。 -
是的,list_places 是我的切入点。谢谢,我会玩循环。我确实想将数据推送到数组中。
-
另外,1) 检查你的
ends,你的BestPlaces::Places班级似乎有一个闲逛; 2)places.collect将失败,因为doc.search("div.text h2.itemName").text中的text将返回一个String对象。
标签: arrays ruby iteration scrape