Net::http

Hi,

I know how to print simple website


require ‘net/http’
Net::HTTP.get_print ‘www.example.com’, ‘/index.html’

I wonder how to print output of searcher for example google?
I mean for example print output from google with filled search filed
with ‘text’?

I know that there is a simple on


require 'net/http'
require 'uri'

res = Net::HTTP.post_form(URI.parse(‘http://www.google’),
{‘search’=>‘text’, ‘max’=>‘50’})
puts res.body

but it dont work as i wanted.

i get:
/usr/lib/ruby/1.8/net/http.rb:1470:in `initialize’: HTTP request path is
empty (ArgumentError)

Please can you help me?

Regards
Beny18241

beny 18241 wrote:


require 'net/http'
require 'uri'

res = Net::HTTP.post_form(URI.parse(‘http://www.google’),
{‘search’=>‘text’, ‘max’=>‘50’})
puts res.body

res = Net::HTTP.post_form(URI.parse(‘http://www.google.com’),
{‘q’=>‘text’, ‘max’=>‘50’})

Take a look at the address bar when you search for ‘text’ with google.
Notice how it has q=text… so ‘search’ would be incorrect I believe.

Regards,

  • Mac

On Nov 30, 2:12 pm, beny 18241 [email protected] wrote:

res = Net::HTTP.post_form(URI.parse(‘http://www.google’),
{‘search’=>‘text’, ‘max’=>‘50’})

You have a few problems here. First, Google’s search won’t accept a
POST. You have to use GET which has a different syntax. Other search
engines might take a POST though.

i get:
/usr/lib/ruby/1.8/net/http.rb:1470:in `initialize’: HTTP request path is
empty (ArgumentError)

You’re using ‘http://www.google’ which isn’t valid. Michael partially
fixed it by adding ‘.com’, but running that will still yield the same
error (empty path).

The POST method has to have a path to post to. At the very least,
you’ll need to use ‘http://www.google.com/’ (notice the slash at the
end). Again, this is assuming the site you’re using will take a POST
– Google won’t and, even if it did, ‘/’ wouldn’t be the right path
(it would be ‘/search’).

Based on your example, it looks as though you’ve found the
documentation for Net::HTTP (at RDoc Documentation ). That
should have everything you need to get a POST or GET working, but that
depends on the site you actually want to hit.

Generally, if you run a search and end up with a URL that ends in ‘?
name1=value1&name2=value2’ (like Google’s ‘&q=hello’), you’ll need
GET. Otherwise, use POST.

You can also view the source of the search form and find the method
that is used (e.g. ). If one
isn’t listed (Google doesn’t list the method), it’s GET.

Finally, if you really want Google results here’s an example of a GET
that will work:

require ‘net/http’

uri = URI.parse(‘hello - Google Search’)

or: uri = URI::HTTP.build({:host => ‘www.google.com’, :path => '/

search’, :query => ‘q=hello’})

Net::HTTP.start(uri.host) do |http|
response = http.get(uri.request_uri)
# do something with the response
end

This is similar to Example 3 in the ‘Getting Document From WWW Server’
section of the Net::HTTP docs.