Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Net::HTTP....please help
nicholas4
6/8/2006 10:02:00 PM
I am new to ruby and am trying to write a simple script that takes a
url and prints the suorce code for the url.
Here's my code:
require 'net/https'
require 'uri'
def get_html(url)
page = Net::HTTP.get_response(URI.parse(url).host,
URI.parse(url).path).body
return page
end
url = "
http://www.cnn.com/2006/POLITICS/06/07/nsa/index....
html_source = get_html(url)
print html_source
When I send the request for the page, how do I specify the user agent?
Thanks!
1 Answer
Tim Heaney
6/9/2006 1:03:00 AM
0
nicholas4@gmail.com writes:
> I am new to ruby and am trying to write a simple script that takes a
> url and prints the suorce code for the url.
The class method get_print does exactly that. That is, I think this
one-liner
ruby -r uri -r net/http -e 'Net::HTTP.get_print(URI.parse(ARGV[-1]))'
http://www.cnn.com/2006/POLITICS/06/07/nsa/...
does what you want.
> When I send the request for the page, how do I specify the user agent?
If you create a Net::HTTP object, then User-Agent is one of many HTTP
headers you can manipulate. I think something like
#!/usr/bin/ruby
require "net/http"
require "uri"
url = URI.parse("
http://www.cnn.com/2006/POLITICS/06/07/nsa/...
")
response = Net::HTTP.start(url.host, url.port) {|http|
http.get(url.path, {'User-Agent' => 'Foozilla/1.0'})
}
puts response.body
ought to do it.
I hope this helps,
Tim
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Net::HTTP....please help
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password