C1ed5a3bfbb74ac76594077b1db30041

This code display jpg image url from a web site.
How can we make the code smarter and better ?
Thanks !

require 'net/http'
require 'uri'
require 'strscan'

html = Net::HTTP.get URI.parse("http://www.apple.com")

s = StringScanner.new(html)

while true 
  txt = s.scan_until(/\.jpg/)
  if not s.matched?
    break
  end
  p /src=.(.*jpg)/.match(txt)[1]
end

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

Jordan Glasner

January 30, 2008, January 30, 2008 16:21, permalink

No rating. Login to rate!

I'm sure my regex could be tuned a bit...

require 'open-uri'

jpg = /img src="(.[^"]*\.jpg)"/

open('http://www.apple.com') do |response|
  html = response.read
  p jpg.match(html)[1]
end
C1ed5a3bfbb74ac76594077b1db30041

Gregory Barborini

January 30, 2008, January 30, 2008 20:27, permalink

No rating. Login to rate!

Nice !
.. and do you know if there's a way to display ALL the jpg image in the page ?

D41d8cd98f00b204e9800998ecf8427e

Jordan Glasner

January 31, 2008, January 31, 2008 01:51, permalink

1 rating. Login to rate!

Somehow I've wrecked my Ruby install today, but I think the following should work

require 'open-uri'

jpg = /img src="(.[^"]*\.jpg)"/

open('http://www.apple.com') do |response|
  match = jpg.match(response.read)
  match.captures.each { |x| print x }
end
C1ed5a3bfbb74ac76594077b1db30041

Gregory Barborini

January 31, 2008, January 31, 2008 03:52, permalink

No rating. Login to rate!

Hum.. I still got one result but thank you so much : i've done some search and here's the result :

require 'open-uri'

open('http://www.apple.com') do |response|
  response.read.scan(/img src="(.[^"]*\.jpg)"/).each { |x| p x.to_s }
end
55502f40dc8b7c769880b10874abc9d0

getopenid.com/ihack

March 19, 2008, March 19, 2008 20:53, permalink

No rating. Login to rate!
require 'open-uri'

open('http://www.apple.com') { |r| r.read.scan(/img src="(.[^"]*\.jpg)"/).each { |x| p x.to_s } }

Your refactoring





Format Copy from initial code

or Cancel