How do I get the page data of an other website somewhere on the web with ruby on rails?
+3
A:
Use Net/HTTP
(for example, read this cheatsheet):
require "net/https"
http = Net::HTTP.new "google.com", 80
request = Net::HTTP::Get.new "/"
response = http.request request
p responde.code
p response.body
floatless
2010-09-04 11:27:09
+1
A:
Net/HTTP
is ok because it's in the standard library, but there are some cool higher-level libraries like rest-client:
RestClient.get 'http://example.com/resource', {:params => {:id => 50, 'foo' => 'bar'}}
tokland
2010-09-04 12:01:14
Thanks for this. It just may be the ticket for a new project of mine.
Mark Thomas
2010-09-04 22:58:33
+1
A:
You can use httparty to just get the data
Sample code (from example):
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')
Nokogiri really excels at parsing that data.. Here's some example code from the Railscast:
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
Dan McNevin
2010-09-04 12:39:18
A:
I like OpenURI myself if it's just to simply get the content no fuss.
Just add require 'open-uri'
to the environment and then do open('http://domain.tld/document.html').read
.
ba
2010-09-04 13:37:59