如何使用Nokogiri仅从元素解析字符串值的一部分?红宝石,机械化 [英] How to parse only part of a string-value from an element using Nokogiri? RUBY, Mechanize

查看:65
本文介绍了如何使用Nokogiri仅从元素解析字符串值的一部分?红宝石,机械化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从字符串中提取数字? 如果xpath是'td [5] p/@ title'

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>

我需要从标题属性中提取字符串值"6 en su sucusal"仅数字6

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

推荐答案

html中提供一些HTML,您将执行以下操作:

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]/, '') }

然后,您将在numbers数组中包含数字.当然,您必须调整XPath和正则表达式以匹配您的真实数据,但是基本技术应该很清楚.

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.

花些时间使用 Nokogiri文档和教程可能会富有成果.

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

这篇关于如何使用Nokogiri仅从元素解析字符串值的一部分?红宝石,机械化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆