Monday, March 28, 2011

Search Web Page Content

How do you search the Web Page Source in ruby Hard to explain, but heres a 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
From stackoverflow
  • A quick look at Google gave me this: http://snippets.dzone.com/posts/show/2430

  • Directly porting your code:

    require 'net/http'
    word = 'How to ask'
    source = Net::HTTP.get(URI.parse('http://stackoverflow.com/'))
    if source.match(word)
        puts "Found #{word}"
    end
    

    If you want to do things like follow redirects, you'll want to read the documentation.

0 comments:

Post a Comment