Display data scraped from Nokogiri in Rails?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 12:03:57

问题


I recently started learning Rails, and am trying to build a simple application which scrapes football fixtures from a website and displays the data in my index.html. Users can then try to predict the scoreline of the fixtures.

I managed to scrape the data into a fixtures.rb file using Nokogiri:

require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open("http://www.bbc.co.uk/sport/0/football/21784836"))
doc.css("tr.row2").each do |item|
  puts item.at_css("td.left.first p").text
end

What would be the easiest way to add this into a Ruby on Rails application? Can I put this into the controller? I'm having trouble pasting the text in full as a view.


回答1:


Add it to an array and then use it in your controller.

@football = []
doc.css("tr.row2").each do |item|
  @football << item.at_css("td.left.first p").text
end

Then in your view you can reference the contents:

<% if @football %>
  <ul>
    <% @football.each do |foot| %>
      <li><%= foot %></li>
    <% end %>
  </ul>
<% end %>


来源:https://stackoverflow.com/questions/15487198/display-data-scraped-from-nokogiri-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!