Nokogiri 在视图中显示数据 [英] Nokogiri displaying data in view

查看:42
本文介绍了Nokogiri 在视图中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图弄清楚如何显示我在应用程序/html 中抓取的文本和图像.这是我的 app/scrape2.rb 文件

Trying to figure out how display the text and images I have scraped in my application/html. Here is my app/scrape2.rb file

require 'nokogiri'
require 'open-uri'

url = "https://marketplace.asos.com/boutiques/independent-label"

doc = Nokogiri::HTML(open(url))

label = doc.css('#boutiqueList')

@label = label.css('#boutiqueList img').map { |l| p l.attr('src') }
@title = label.css("#boutiqueList .notranslate").map { |o| p o.text }

这是控制器:

class PagesController < ApplicationController
    def about
        #used to change the routing to /about
    end

      def index
         @label = label.css('#boutiqueList img').map { |l| p l.attr('src') }
         @title = label.css("#boutiqueList .notranslate").map { |o| p o.text }
    end

end

最后是 label.html.erb 页面:

and finally the label.html.erb page:

<% @label.each do |image| %>
<%= image_tag image %>
<% end %>

我是否需要其他方法,而不是正确存储数组?

do I need some other method, not storing the arrays properly?

推荐答案

您的控制器需要自己加载数据,或者以某种方式从 scrape2.rb 中提取数据.除非指定(包含、扩展等),否则控制器无权访问其他文件.

Your controller needs to load the data itself, or somehow pull the data from scrape2.rb. Controllers do not have access to other files unless specified (include, extend, etc).

require 'nokogiri'
require 'open-uri'

class PagesController < ApplicationController

  def index 

     # Call these in your controller:
     url = "https://marketplace.asos.com/boutiques/independent-label"
     doc = Nokogiri::HTML(open(url))
     label = doc.css('#boutiqueList')

     @label = label.css('#boutiqueList img').map { |l| p l.attr('src') }
     @title = label.css("#boutiqueList .notranslate").map { |o| p o.text }
  end
end

这篇关于Nokogiri 在视图中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆