S
Sergei Maertens
Good evening
First I'll mention I have used the search function and found some useful
topics, but I still don't really find a solution due to a lack of Ruby
and Hpricot/Xpath knowlegde.
The problem is the following: from
http://users.telenet.be/weerstation.drongen/index.htm/Current_Vantage_Pro.htm
I need to scrape the temperature and Today's Rain values (need those for
Engineering Project). With Xpather and Firebug I looked up the Xpath to
the Temperature values:
/html/body/table/tbody/tr[3]/td[2]/font/strong/small/font (as Xpather
says so).
But when I try to print the value in Ruby, I got nil.
Here is my code:
---------------------------------------------------------------------------
#!/usr/bin/ruby
require 'rubygems'
require 'open-uri'
require 'hpricot'
@url="http://users.telenet.be/weerstation.drongen/index.htm/Current_Vantage_Pro.htm"
xpath = "/html/body/table/tbody/tr[3]/td[2]/font/strong/small/font"
@response=""
begin
open(@url) {|file|
puts "Fetched Document: #{file.base_uri}"
@response = file.read
}
doc = Hpricot(@response)
puts (doc/"#{xpath}").inner_html
rescue Exception => e
puts e
end
---------------------------------------------------------------------------
Since this returned nil, I decided to look up where I got nil returned.
Apparently /html/body/table/tbody is too far, because /html/body/table
still returns an output and tbody returns nil.
I've read that I should try to rebuild the path now, but I really don't
find a way how to do this. This is only my second serious Ruby script
(only the beginning actually) and the first time I used Hpricot.
I'm looking forward to replies, and I'm sorry to bother you with yet
another Hpricot-nil topic, but I'm kinda hopeless because of my
deadline...
Kind regards,
Sergei
First I'll mention I have used the search function and found some useful
topics, but I still don't really find a solution due to a lack of Ruby
and Hpricot/Xpath knowlegde.
The problem is the following: from
http://users.telenet.be/weerstation.drongen/index.htm/Current_Vantage_Pro.htm
I need to scrape the temperature and Today's Rain values (need those for
Engineering Project). With Xpather and Firebug I looked up the Xpath to
the Temperature values:
/html/body/table/tbody/tr[3]/td[2]/font/strong/small/font (as Xpather
says so).
But when I try to print the value in Ruby, I got nil.
Here is my code:
---------------------------------------------------------------------------
#!/usr/bin/ruby
require 'rubygems'
require 'open-uri'
require 'hpricot'
@url="http://users.telenet.be/weerstation.drongen/index.htm/Current_Vantage_Pro.htm"
xpath = "/html/body/table/tbody/tr[3]/td[2]/font/strong/small/font"
@response=""
begin
open(@url) {|file|
puts "Fetched Document: #{file.base_uri}"
@response = file.read
}
doc = Hpricot(@response)
puts (doc/"#{xpath}").inner_html
rescue Exception => e
puts e
end
---------------------------------------------------------------------------
Since this returned nil, I decided to look up where I got nil returned.
Apparently /html/body/table/tbody is too far, because /html/body/table
still returns an output and tbody returns nil.
I've read that I should try to rebuild the path now, but I really don't
find a way how to do this. This is only my second serious Ruby script
(only the beginning actually) and the first time I used Hpricot.
I'm looking forward to replies, and I'm sorry to bother you with yet
another Hpricot-nil topic, but I'm kinda hopeless because of my
deadline...
Kind regards,
Sergei