我有我需要的所有代碼,然後將結果打印到控制檯,但是我對如何在應用程序中使用它感到困惑。在Rails應用程序中使用Nokogiri
它應該工作的方式是通過list#new
操作,我爲一個參數輸入用戶輸入:url
。然後將該URL傳遞給刮取代碼,該代碼獲取所有附加參數並將所有內容添加到Postgres表格中。使用所有這些新獲取的數據,將呈現新的list
。
,我有問題:
列表控制器:
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? . . .
我認爲這將進入模型/ 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
,我有一些問題:
如何正確引入
:url
到HTML(open...
?現在我有它的方式拋出no implicit conversion of Symbol into String
錯誤。:url
與:name
和:author
應該被保存到一個數據庫條目中的部分是非常模糊的。
任何關於這個東西的文章建議總是歡迎。
是否創建了一個列表,而不管是否刮取?或者你需要刮臉才能保存清單? – PericlesTheo 2014-09-30 21:43:44
稍後,我將對提交的url是否有效進行檢查(打開的頁面可以使用代碼進行刮擦)。所以不行,如果不能通過抓取檢索額外的參數,則不會保存新的列表條目。 – sivanes 2014-09-30 21:50:03
請參閱下面的答案。這是一個粗略的估計,你需要什麼。 – PericlesTheo 2014-09-30 22:00:00