| Class | DemoController |
| In: |
app/controllers/demo_controller.rb
|
| Parent: | ActionController::Base |
# File app/controllers/demo_controller.rb, line 24
24: def search_google
25: myurl = "http://www.google.com"
26: mysearch_expression = "div.gb2/a[@href^='http']"
27: hpricot_object = get_my_hp_elem(myurl)
28: @somehtml = hpricot_object.search(mysearch_expression).to_html
29: end
Returns an Hpricot object from HTML obtained by get_my_html_from_open_uri()
# File app/controllers/demo_controller.rb, line 47
47: def get_my_hp_elem(u)
48: h0 = Hpricot(get_my_html_from_open_uri(u))
49: # remove crap
50: # (h0/"script").remove
51: return h0
52: end
Returns raw HTML. Usually it gets passed to get_my_hp_elem()
# File app/controllers/demo_controller.rb, line 35
35: def get_my_html_from_open_uri(u)
36: hdrs = {"User-Agent"=>"Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1", "Accept-Charset"=>"utf-8", "Connection"=>"Keep-Alive", "Accept"=>"text/html"}
37: my_html = ""
38: begin
39: open(u, hdrs).each {|s| my_html << s}
40: rescue
41: my_html = "<html><body><p /><b>hello world</b></body></html>"
42: end
43: return my_html
44: end