Reputation: 73
I am attempting to download a page from Wikipedia. For such a task, I am using gems. When using net/http, all I get is an empty string. So I tried with open-uri and it works fine.
Nevertheless, I prefer the first option because it gives me a much more explicit control; but why is it returning an empty string?
class Downloader
attr_accessor :entry, :url, :page
def initialize
# require 'net/http'
require 'open-uri'
end
def getEntry
print "Article name? "
@entry = gets.chomp
end
def getURL(entry)
if entry.include?(" ")
@url = "http://en.wikipedia.org/wiki/" + entry.gsub!(/\s/, "_")
else
@url = "http://en.wikipedia.org/wiki/" + entry
end
@url.downcase!
end
def getPage(url)
=begin THIS FAULTY SOLUTION RETURNS AN EMPTY STRING ???
connector = URI.parse(url)
connection = Net::HTTP.start(connector.host, connector.port) do |http|
http.get(connector.path)
end
puts "Body:"
@page = connection.body
=end
@page = open(url).read
end
end
test = Downloader.new
test.getEntry
test.getURL(test.entry)
test.getPage(test.url)
puts test.page
P.S.: I am an autodidact programmer so the code might not fit good practices. My apologies.
Upvotes: 2
Views: 619
Reputation: 226
Because your request return 301 Redirect
(check connection.code
value), you should follow redirect manually if you are using net/http
. Here is more details.
Upvotes: 1