ruby - How to specify multiple column by xpath -


i want multiple table data html this:

html = <<eof <table>   <tr>     <td>1</td>     <td>2</td>     <td>3</td>   </tr>   <tr>     <td>4</td>     <td>5</td>     <td>6</td>   </tr> </table> eof 

i want 2 data like:

noko = nokogiri::html(html) noko.xpath("//tr[1]/td[2]").text #=> "2" noko.xpath("//tr[1]/td[3]").text #=> "3" 

what expect code "23", return "123". how can "23" using xpath?

noko.xpath("//tr[1]/td[2 , 3]").text 

there multiple ways of solving problem. 1 :

require 'nokogiri'  html = <<eof <table>   <tr>     <td>1</td>     <td>2</td>     <td>3</td>   </tr>   <tr>     <td>4</td>     <td>5</td>     <td>6</td>   </tr> </table> eof  noko = nokogiri::html(html) p noko.xpath("//tr[1]/td[position()= 2 or position() = 3]").map(&:text).join  # => 23 # way p noko.xpath("//tr[1]/td[2]", "//tr[1]/td[3]").map(&:text).join # => 23 

Comments

Post a Comment

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -