Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何使用Nokogiri从图像标记中刮取文本?_Ruby_Xml_Image_Nokogiri - Fatal编程技术网

Ruby 如何使用Nokogiri从图像标记中刮取文本?

Ruby 如何使用Nokogiri从图像标记中刮取文本?,ruby,xml,image,nokogiri,Ruby,Xml,Image,Nokogiri,我需要从如下格式的图像标记列表中获取文本: <img src="/images/TextImage.ashx?text=Richmond" style="border-width:0px;" class=""> 当我在Nokogiri中输入XPath时,我得到: [#<Nokogiri::XML::Element:0x80513954 name="img" attributes=[#<Nokogiri::XML::Attr:0x805138dc name="src"

我需要从如下格式的图像标记列表中获取文本:

<img src="/images/TextImage.ashx?text=Richmond" style="border-width:0px;" class="">

当我在Nokogiri中输入XPath时,我得到:

[#<Nokogiri::XML::Element:0x80513954 name="img" attributes=[#<Nokogiri::XML::Attr:0x805138dc name="src" value="/images/TextImage.ashx?text=Richmond">, #<Nokogiri::XML::Attr:0x805138b4 name="style" value="border-width:0px;">]>] 
[#]

我有没有办法让野口吉把“里士满”还给你?我正在寻找一种方法,它将在某个字符串之后返回文本。如果没有方法只获取“Richmond”,如何获取它以返回值?

您可以使用xpath表达式提取
src
属性,如

src = doc.at_xpath '//img/@src'
之后,您需要从属性中提取名称,可能需要使用正则表达式

例如(这可能需要更多的参与,具体取决于HTML页面的
src
属性中可能采用的格式):


当我这样做时,我得到了这个:#@Guyana.Hand是的,这是属性节点。您需要使用正则表达式从中提取地名(您可以将其直接与正则表达式一起使用,就像它是一个字符串一样,其
to_str
方法返回属性值)。请注意,它的格式与您问题中的格式不同,因此您的整个正则表达式需要比我在回答中使用的简单正则表达式更复杂。谢谢!我不知道如何使用正则表达式。你能推荐一个教程吗?
/\?text=(.*)/ =~ src
puts $1