Ruby on rails 使用ruby on rails从网站获取html
如何使用ruby on rails在web上的某处获取其他网站的页面数据?使用Ruby on rails 使用ruby on rails从网站获取html,ruby-on-rails,ruby,Ruby On Rails,Ruby,如何使用ruby on rails在web上的某处获取其他网站的页面数据?使用Net/HTTP(例如,阅读): 在标准库中提供,这是一个优点,但也有一些很酷的高级库,您可以查看,例如: 您可以使用仅获取数据 示例代码(来自): 非常擅长解析这些数据。。下面是来自的一些示例代码: 我自己也喜欢OpenURI,如果它只是简单地获取内容,而不需要大惊小怪的话 只需将require'openuri'添加到环境中,然后执行open('http://domain.tld/document.html)。请阅读
Net/HTTP
(例如,阅读):
在标准库中提供,这是一个优点,但也有一些很酷的高级库,您可以查看,例如:
您可以使用仅获取数据
示例代码(来自):
非常擅长解析这些数据。。下面是来自的一些示例代码:
我自己也喜欢OpenURI,如果它只是简单地获取内容,而不需要大惊小怪的话
只需将
require'openuri'
添加到环境中,然后执行open('http://domain.tld/document.html)。请阅读
谢谢。这可能是我一个新项目的门票。
require "net/https"
http = Net::HTTP.new "google.com", 80
request = Net::HTTP::Get.new "/"
response = http.request request
puts response.code
puts response.body
RestClient.get('http://example.com/resource', params: {x: "1", y: "2"})
require File.join(dir, 'httparty')
require 'pp'
class Google
include HTTParty
format :html
end
# google.com redirects to www.google.com so this is live test for redirection
pp Google.get('http://google.com')
puts '', '*'*70, ''
# check that ssl is requesting right
pp Google.get('https://www.google.com')
url = "http://www.walmart.com/search/search-ng.do?search_constraint=0&ic=48_0&search_query=batman&Find.x=0&Find.y=0&Find=Find"
doc = Nokogiri::HTML(open(url))
puts doc.at_css("title").text
doc.css(".item").each do |item|
title = item.at_css(".prodLink").text
price = item.at_css(".PriceCompare .BodyS, .PriceXLBold").text[/\$[0-9\.]+/]
puts "#{title} - #{price}"
puts item.at_css(".prodLink")[:href]
end