How to parse only part of a string-value from an element using Nokogiri? RUBY, Mechanize

只愿长相守 提交于 2020-01-17 05:17:05

问题


How do I extract numbers off a string ? if xpath is 'td[5]p/@title'

HTML :

<td valign="top" align="center">
  <p title="6 en su sucursal" style="margin-top: 0px; margin-bottom:0px; cursor:hand">   
   <b>10</b>
  </p>
</td>

I need to extract from the title attribute string-value "6 en su sucusal" only number 6


回答1:


Give some HTML inside html, you'd do something like this:

doc     = Nokogiri::HTML(html)
numbers = doc.xpath('//p[@title]').collect { |p| p[:title].gsub(/[^\d]/, '') }

Then you'll have the numbers in the numbers array. You'll have to adjust the XPath and regular expression to match your real data of course but the basic technique should be clear.

A bit of time with the Nokogiri documentation and tutorials might be fruitful.



来源:https://stackoverflow.com/questions/6449208/how-to-parse-only-part-of-a-string-value-from-an-element-using-nokogiri-ruby-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!