Prev: DESTROY in ruby
Next: Help with gem ruby mysql error
From: Soichi Ishida on 21 Dec 2009 02:36 ruby 1.8.6 Ubuntu 8.04 nokogiri 1.4.0 Hi. I am trying to get a number(Nikkei 225) from the web site, "Google Finance". It's a number that indicates the average of major stock prices, which changes in real-time. http://www.google.com/finance?q=INDEXNIKKEI:.N225 here is the code I tried. ------------------------------------------------- require 'rubygems' require 'nokogiri' require 'open-uri' url = "http://www.google.com/finance?q=INDEXNIKKEI:.N225" doc = Nokogiri::HTML(open(url).read) path1 = "/html/body/div[@id='ref_15513676_l']" p doc.xpath(path1).text() ------------------------------------------------- but it gives nothing. But no error, though. The full xpath for the number, which appears big and bold in the near top of the page is ------------------------------------------------- /html/body/div[@id='outer-wrapper']/div[@id='body-wrapper']/div[6]/div[@id='rt-content']/div/div[2]/div[2]/div[@id='market-data-div']/div[@id='price-panel']/div[1]/span/span[@id='ref_15513676_l']/span[1] ------------------------------------------------- then I guessed that the important part would be the last one followed from "[@id..." in comparison to the Dow average which is below. ------------------------------------------------- /html/body/div[@id='outer-wrapper']/div[@id='body-wrapper']/div[6]/div[@id='rt-content']/div/div[2]/div[2]/div[@id='market-data-div']/div[@id='price-panel']/div[1]/span/span[@id='ref_983582_l'] ------------------------------------------------- What's wrong with my code? Could anyone help me out? soichi -- Posted via http://www.ruby-forum.com/.
From: Edward Middleton on 21 Dec 2009 02:45 Soichi Ishida wrote: > here is the code I tried. > ------------------------------------------------- > require 'rubygems' > require 'nokogiri' > require 'open-uri' > > url = "http://www.google.com/finance?q=INDEXNIKKEI:.N225" > > doc = Nokogiri::HTML(open(url).read) > > path1 = "/html/body/div[@id='ref_15513676_l']" > Your XPATH is wrong try path1 = "/html/body/div//span[@id='ref_15513676_l']" Edward
From: Soichi Ishida on 21 Dec 2009 02:53 It worked! Thanks Edward for such a quick reply. I appreciate it. soichi -- Posted via http://www.ruby-forum.com/.
|
Pages: 1 Prev: DESTROY in ruby Next: Help with gem ruby mysql error |