tags:

views:

73

answers:

2

How do you search a Websites source code with ruby, hard to explain but heres the code for doing it in python

import urllib2, re
word = "How to ask"
source = urllib2.urlopen("http://stackoverflow.com").read()
if re.search(word,source):
     print "Found it "+word
+3  A: 

Here's one way:

require 'open-uri'
word = "How to ask"
open('http://stackoverflow.com') do |f|
  puts "Found it #{word}" if f.read =~ /#{word}/
end
jcrossley3
+2  A: 

If all you want to do is search jcrossley3 gave you your answere. If you want to do something more complicated you should look at an HTML parser that can let you treat the website like a DOM Tree. Have a look at why´s great hpricot gem to do just that.

 require 'hpricot'
 require 'open-uri'
 doc = open("http://qwantz.com/") { |f| Hpricot(f) }
 doc.search("//p[@class='posted']")
 (doc/"p/a/img").each do |img|
   puts img.attributes['class']
 end
LDomagala