Net::HTTP....please help

N

nicholas4

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"
html_source = get_html(url)
print html_source


When I send the request for the page, how do I specify the user agent?

Thanks!
 
T

Tim Heaney

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/index.html

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/index.html")

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
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,226
Latest member
KristanTal

Latest Threads

Top