2014-09-30 65 views
0

我有我需要的所有代碼,然後將結果打印到控制檯,但是我對如何在應用程序中使用它感到困惑。在Rails應用程序中使用Nokogiri

它應該工作的方式是通過list#new操作,我爲一個參數輸入用戶輸入:url。然後將該URL傳遞給刮取代碼,該代碼獲取所有附加參數並將所有內容添加到Postgres表格中。使用所有這些新獲取的數據,將呈現新的list

,我有問題:

  1. 列表控制器:

    class UsersController < ApplicationController 
        . 
        . 
        . 
        def create 
        @list = List.new (#what goes in here? 
             #only one param comes from the user 
        if @list.save 
         #how to set it up so that the save is successful 
         #only if the extra params have been scraped? 
        . 
        . 
        . 
    
  2. 我認爲這將進入模型/ list.rb:

    class List < ActiveRecord::Base 
    
    require 'open-uri' 
    
    url = #assuming that the url is proper and for something this code is supposed to scrape 
         #is it better to add the url to db first or send it straight from the input 
         #and how is that defined here 
    doc = Nokogiri::HTML(open(url)) 
    . 
    . 
    . 
    

請問你能給我一些指導嗎?


services文件:

class ScrapingService 

    require 'open-uri' 
    require 'nokogiri' 

    def initialize(list) 
    @list = list 
    end 

    url = :url 
    doc = Nokogiri::HTML(open(url)) 
    name = doc.at_css(".currentverylong").text 
    author = doc.at_css(".user").text 

    def scraped_successfully? 
    if name != nil && author != nil 
     true 
    else 
     false 
    end 
    end 

    private 

    attr_reader :list 

end 

,我有一些問題:

  1. 如何正確引入:urlHTML(open...?現在我有它的方式拋出no implicit conversion of Symbol into String錯誤。

  2. :url:name:author應該被保存到一個數據庫條目中的部分是非常模糊的。

任何關於這個東西的文章建議總是歡迎。

+0

是否創建了一個列表,而不管是否刮取?或者你需要刮臉才能保存清單? – PericlesTheo 2014-09-30 21:43:44

+0

稍後,我將對提交的url是否有效進行檢查(打開的頁面可以使用代碼進行刮擦)。所以不行,如果不能通過抓取檢索額外的參數,則不會保存新的列表條目。 – sivanes 2014-09-30 21:50:03

+0

請參閱下面的答案。這是一個粗略的估計,你需要什麼。 – PericlesTheo 2014-09-30 22:00:00

回答

0

應用程序/控制器/ lists_controller.rb

class UsersController < ApplicationController 
    def create 
    @list = List.new(list_params) 
    if @list.save 
     redirect_to @list 
    else 
     render :new 
    end 

    private 

    #Assuming that you are using Rails 4 or the strong_params gem 
    def list_params 
    params.require(:list).permit(:url) 
    end 
end 

應用程序/模型/ list.rb

class List < ActiveRecord::Base 
    # This runs only when you try to create a list. If you want to run this 
    # validation when the user updates it, the remove the on: :create 
    before_validation :ensure_website_is_scrapable, on: :create 

    private 

    def ensure_website_is_scrapable 
    if ScrapingService.new(self).scraped_successfully? 
     true 
    else 
     errors.add(:url, 'The website is not scrapable') 
    end 
    end 
end 

應用程序/服務/ scraping_service.rb

class ScrapingService 
    def initialize(list) 
    @list = list 
    end 

    def scraped_successfully? 
    # Do the scraping logic here and return true if it was successful or false otherwise 
    # Of course split the implementation to smaller methods 
    end 

    private 

    attr_reader :list 
end 
+0

真棒,我想是我正在尋找。我需要幾天的時間來填補所有空白,然後開始運行,因爲我仍然是初學者,但我會繼續檢查這一個。 – sivanes 2014-09-30 22:06:30

+0

只要你需要什麼,我就會很高興幫助。 – PericlesTheo 2014-09-30 22:07:05

+0

我明白, – sivanes 2014-09-30 22:09:27