From: Robert Klemme Date: 2012-10-31T18:12:30+09:00 Subject: Re: Nokogiri Xpath: need to write "\n" for every table element On Wed, Oct 31, 2012 at 6:20 AM, Soichi Ishida wrote: > Rails 1.9.3 > > For http://en.wikipedia.org/wiki/List_of_airports_by_IATA_code:_A > I would like to make a list of airport names using Nokogiri. > > The following code seems to work but it does not insert "\n" as I wish. > > Can you tell me why? > > > > require 'open-uri' > require 'nokogiri' > > test_url = > "http://en.wikipedia.org/wiki/List_of_airports_by_IATA_code:_A" > > url_list_file = "list_page_url.txt" > test_xpath = "//tr" > output_file = "list_airport_names_wiki_url.txt" > > test = Nokogiri::HTML(open(test_url)) > File.open(output_file, "a") {|f| > test.xpath(test_xpath).each do |e| > f.write e.xpath("//td[3]/a").text + "\n" #### HERE!!! #### > end > } First of all the XPath looks suspicious: you certainly want only "td" elements nested below the current "tr". So you should use any of td[3]/a .//td[3]/a Otherwise the first selection is useless because //td[3]/a will select all "a" children of the third "td" in the document. Also, e.xpath will return a NodeSet which, when converted via #text, will lead to surprising results: irb(main):026:0> puts dom
abc
=> nil irb(main):027:0> dom.xpath('//*') => [#]>]>, #]>] irb(main):028:0> dom.xpath('//*').text => "abcabc" Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/