open-uri

How to get HTTP headers before downloading with Ruby's OpenUri

£可爱£侵袭症+ 提交于 2019-11-28 00:00:39
问题 I am currently using OpenURI to download a file in Ruby. Unfortunately, it seems impossible to get the HTTP headers without downloading the full file: open(base_url, :content_length_proc => lambda {|t| if t && 0 < t pbar = ProgressBar.create(:total => t) end }, :progress_proc => lambda {|s| pbar.progress = s if pbar }) {|io| puts io.size puts io.meta['content-disposition'] } Running the code above shows that it first downloads the full file and only then prints the header I need. Is there a

What is the difference between Ruby's 'open-uri' and 'Net:HTTP' gems?

江枫思渺然 提交于 2019-11-27 17:21:46
问题 It seems like both of these gems perform very similar tasks. Can anyone give examples of where one gem would be more useful than the other? I don't have specific code that I'm referring to, I'm more wondering about general use cases for each gem. I know this is a short question, I will fill in the blanks upon request. Thanks. 回答1: The reason they look like they perform similar tasks is OpenURI is a wrapper for Net::HTTP, Net::HTTPS, and Net::FTP. Usually, unless you feel you need a lower

Ruby open-uri redirect forbidden

落花浮王杯 提交于 2019-11-27 15:45:43
问题 I have this simple html parser(for learning purposes) that I have been working on.: require 'open-uri' puts "Enter URL to parse HTML: " url = gets.chomp puts "Enter tag to parse from: " tag = gets.chomp response = open(url).read title1 = response.index(tag) title2 = response.index(tag.insert(1,'/')) -1 result = response[(title1 + tag.length - 1)..title2] print result and when I input http://twitter.com , I get this error message: ERROR: `open_loop': redirection forbidden: http://twitter.com -

Fast way to get remote image dimensions

落花浮王杯 提交于 2019-11-27 11:48:20
问题 I'm using the imagesize gem to check the sizes of remote images and then only push images that are big enough into an array. require 'open-uri' require 'image_size' data = Nokogiri::HTML(open(url)) images = [] forcenocache = Time.now.to_i # No cache because jquery load event doesn't fire for cached images data.css("img").each do |image| image_path = URI.join(site, URI.encode(image[:src])) open(image_path, "rb") do |fh| image_size = ImageSize.new(fh.read).get_size() unless image_size[0] < 200

Nokogiri, open-uri, and Unicode Characters

我怕爱的太早我们不能终老 提交于 2019-11-27 03:41:10
I'm using Nokogiri and open-uri to grab the contents of the title tag on a webpage, but am having trouble with accented characters. What's the best way to deal with these? Here's what I'm doing: require 'open-uri' require 'nokogiri' doc = Nokogiri::HTML(open(link)) title = doc.at_css("title") At this point, the title looks like this: Rag\303\271 Instead of: Ragù How can I have nokogiri return the proper character (e.g. ù in this case)? Here's an example URL: http://www.epicurious.com/recipes/food/views/Tagliatelle-with-Duck-Ragu-242037 When you say "looks like this," are you viewing this value

Nokogiri, open-uri, and Unicode Characters

不打扰是莪最后的温柔 提交于 2019-11-26 12:40:40
问题 I\'m using Nokogiri and open-uri to grab the contents of the title tag on a webpage, but am having trouble with accented characters. What\'s the best way to deal with these? Here\'s what I\'m doing: require \'open-uri\' require \'nokogiri\' doc = Nokogiri::HTML(open(link)) title = doc.at_css(\"title\") At this point, the title looks like this: Rag\\303\\271 Instead of: Ragù How can I have nokogiri return the proper character (e.g. ù in this case)? Here\'s an example URL: http://www.epicurious