Net::http

B

beny 18241

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
 
M

Michael Linfield

beny said:
---
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
 
Y

yermej

   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 http://ruby-doc.org/stdlib ). 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. <form method="POST" action='/a_path'>). 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('http://www.google.com/search?q=hello')
# 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top