问题
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