웹 사이트의 콘텐츠를 가져 와서 제출 양식에 입력하고 해당 정보를 json으로 저장하고 싶습니다. 내 db에 저장할 수 있습니다. HTTParty를 사용하려하지만 데이터를 가져 오는 방법을 구현하는 방법이 확실하지 않습니다. 여기까지 내가 지금까지 가지고있는 것이있다.httparty를 사용하여 웹 사이트에서 콘텐츠를 얻으려면 어떻게해야합니까?
내 controller.rb입니다 컨트롤러
class UrlsController < ApplicationController
before_action :set_url, only: [:show, :edit, :update, :destroy]
#require "addressable/uri"
#Addressable::URI.parse(url)
# GET /urls
# GET /urls.json
def index
@urls = Url.all
end
# GET /urls/1
# GET /urls/1.json
def show
end
# GET /urls/new
def new
@url = Url.new
end
# GET /urls/1/edit
def edit
end
def uri?(string)
uri = URI.parse(string)
%w(http https).include?(uri.scheme)
rescue URI::BadURIError
false
rescue URI::InvalidURIError
false
end
# POST /urls
# POST /urls.json
def create
@url = Url.new(url_params)
@app_url = params[:url]
respond_to do |format|
if @url.save
format.html { redirect_to @url, notice: 'Url was successfully created.' }
format.json { render action: 'show', status: :created, location: @url }
wordcount
else
format.html { render action: 'new' }
format.json { render json: @url.errors, status: :unprocessable_entity }
end
end
end
def wordcount
# Choose the URL to visit
@app_url = @url
@words = HTTParty.get(@app_url)
# Trick to pretty print headers
@wordcount = Hash[*@words]
end
# PATCH/PUT /urls/1
# PATCH/PUT /urls/1.json
def update
respond_to do |format|
if @url.update(url_params)
format.html { redirect_to @url, notice: 'Url was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @url.errors, status: :unprocessable_entity }
end
end
end
# DELETE /urls/1
# DELETE /urls/1.json
def destroy
@url.destroy
respond_to do |format|
format.html { redirect_to urls_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_url
@url = Url.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def url_params
params.require(:url).permit(:url)
end
end
. 줄에서나쁜 인수 (예상 URI 개체 또는 URI 문자열) '을 받고 있어요 유효한 URL 폼에 URL을 넣고 URL에서 원하는 콘텐츠를 가져 와서 그 정보를 저장해야합니다. 이 같은
귀하의 요점은, 왜 스택에 코드를 넣지 유효하지 않은 것 같다? – neo
왜 데이터를 JSON에 저장 한 다음 데이터베이스에 저장해야하나요? 따라서 데이터를 다시 사용하기가 어렵습니다. 데이터에서 JSON을 쉽게 만들 수 있습니다. –
나중에 입력하고 볼 수있는 각 사이트에서 단어 목록을 가져오고 싶습니다. 가장 좋은 방법이 아닌 해시에 저장하고 있습니까? – user3443619