Reputation: 1031
require 'net/http'
1.upto(99) do |novel|
puts "Downloading ##{novel}..."
Net::HTTP.start("http://www.nbc.com") do |http|
resp = http.get("/heroes/novels/downloads/Heroes_novel_0#{novel}.pdf")
open("Heroes_novel_#{novel}.pdf", "w") do |file|
file.write(resp.body)
end
end
puts "Next..."
puts
end
puts "Okay, Sneak it's done!"
When I run my script I get these errors:
C:/Ruby192/lib/ruby/1.9.1/net/http.rb:644:in
initialize': getaddrinfo: No such host is known. (SocketError) from C:/Ruby192/lib/ruby/1.9.1/net/http.rb:644:in
open' from C:/Ruby192/lib/ruby/1.9.1/net/http.rb:644:inblock in connect' from C:/Ruby192/lib/ruby/1.9.1/timeout.rb:44:in
timeout' from C:/Ruby192/lib/ruby/1.9.1/timeout.rb:89:intimeout' from C:/Ruby192/lib/ruby/1.9.1/net/http.rb:644:in
connect' from C:/Ruby192/lib/ruby/1.9.1/net/http.rb:637:indo_start' from C:/Ruby192/lib/ruby/1.9.1/net/http.rb:626:in
start' from C:/Ruby192/lib/ruby/1.9.1/net/http.rb:490:instart' from heroes.rb:5:in
block in ' from heroes.rb:3:inupto' from heroes.rb:3:in
'
Upvotes: 1
Views: 725
Reputation: 13645
You need to parse the proper parameters into start()
. It takes a host and a port. The easiest way to do this is to make a URI object first.
uri = URI('http://www.nbc.com')
Net::HTTP.start(uri.host, uri.port) do |http|
#do some get requests and handle it
end
This translates to Net::HTTP.start("www.nbc.com", 80)
Upvotes: 4