ruby - xpath selecting text from link in <td> & text from <td> -
i have following code works well:
rows = diary_html.xpath('//*[@id="main"]/div[2]/table/tbody/tr') food_diary = rows.collect |row| detail = {} [ ["food", 'td[1]/text()'], ["calories", 'td[2]/text()'], ["carbs", 'td[3]/text()'], ["fat", 'td[4]/text()'], ["protein", 'td[5]/text()'], ["cholest", 'td[6]/text()'], ].each |name, xpath| detail[name] = row.at_xpath(xpath).to_s.strip end detail end however "food" td not include text, link want text.
i know can use 'td[1]/a/text()'to link text, how do both?
'td[1]/a/text()' or 'td[1]/text()'
edited - added snippet.
i trying include <tr class="meal_header"> <td class="first alt">breakfast</td> on first row, lines other regular tds on other rows whilst excluding td1 on bottom row.
<tr class="meal_header"> <td class="first alt">breakfast</td> <td class="alt">calories</td> <td class="alt">carbs</td> <td class="alt">fat</td> <td class="alt">protein</td> <td class="alt">sodium</td> <td class="alt">sugar</td> </tr> <tr> <td class="first alt"> <a onclick="showeditfood(3992385560);" href="#">hovis (uk - white bread (40g) toasted flora light marg, 2 slice</a> </td> <td>262</td> <td>36</td> <td>9</td> <td>7</td> <td>0</td> <td>3</td> </tr> <tr class="bottom"> <td class="first alt" style="z-index: 10"> <a href="/food/add_to_diary?meal=0" class="add_food">add food</a> <div class="quick_tools"> <a href="#quick_tools_0" class="toggle_diary_options">quick tools</a> <div id="quick_tools_0" class="quick_tools_options hidden"> <ul> <li><a onclick="showlightbox(200, 250, '/food/quick_add?meal=0&date=2013-04-15'); return false;">quick add calories</a></li> <li><a href="/meal/new?meal=0">remember meal</a></li> <li><a href="/food/copy_meal?date=2013-04-15&from_date=2013-04-14&meal=0&username=nickwild1">copy yesterday</a></li> <li><a href="#recent_meals_0" class="toggle_diary_options">copy date</a></li> <li><a href="#recent_meals_copy_to_0" class="toggle_diary_options">copy date</a></li> </ul> </div> <div id="recent_meals_0" class="recent_meal_options hidden"> <ul id="recent_meal_options_0"> <li class="header">copy date?</li> <li><a href="/food/copy_meal?date=2013-04-15&from_date=2013-04-14&meal=0&username=nickwild1">sunday, april 14</a></li> <li><a href="/food/copy_meal?date=2013-04-15&from_date=2013-04-13&meal=0&username=nickwild1">saturday, april 13</a></li> </ul> </div> </div> </td> <td>285</td> <td>39</td> <td>9</td> <td>10</td> <td>0</td> <td>3</td> <td></td>
the short answer is: use nokogiri::xml::element#text, give text of element plus subelements (your a example).
you can clean code quite bit:
keys = ["food", "calories", "carbs", "fat", "protein", "cholest"] food_diary = rows.collect |row| hash[keys.zip row.search('td').map(&:text)] end and final tip, avoid using xpath html, css nicer.
Comments
Post a Comment