使用Nokogiri获得了正确的节点,但需要进一步搜索

时间:2015-05-15 13:02:50

标签: ruby-on-rails ruby nokogiri scrape

我正在使用它。

doc = Nokogiri::HTML(open(url))
pic = doc.search "[text()*='hiRes']"

获取此脚本节点:

<script type="text/javascript">
var data = {
'colorImages': { 'initial': 
[{"hiRes":"http://ecx.images-joes.com/images
/I/71MBTEP1W9L._UL1500_.jpg","thumb":"http://ecx.images-joes.com/images
/I/41xE2XADIvL._US40_.jpg","large":"http://ecx.images-joes.com/images
/I/41xE2XADIvL.jpg","main":{"http://ecx.images-joes.com/images
/I/71MBTEP1W9L._UX395_.jpg":[395,260],"http://ecx.images-joes.com/images
/I/71MBTEP1W9L._UX500_.jpg":[500,329],"http://ecx.images-joes.com/images
/I/71MBTEP1W9L._UX535_.jpg":[535,352],"http://ecx.images-joes.com/images
/I/71MBTEP1W9L._UX575_.jpg":[575,379]}

并且节点继续从那里开始..

但我唯一需要提取的是包含字符串的整个URL。 &#34; UL1500&#34;或者跟随的网址&#34; hiRes:&#34; .. ex。 http://ecx.images-joes.com/images/I/71MBTEP1W9L.UL1500.jpg

我查了Nokogiri返回的类,它是Nokogiri :: XML :: NodeSet

但是我不确定如何与它互动以获得我需要的东西?

由于

2 个答案:

答案 0 :(得分:0)

呀。它是一个NodeSet,因为它是通用的。

请参阅:http://www.rubydoc.info/github/sparklemotion/nokogiri/master/Nokogiri/XML/NodeSet#children-instance_method

在这种情况下,您可以尝试:

pic.children.first.content

答案 1 :(得分:0)

我从使用Nokogiri转到正则表达式......但最终找到了这个并且它就像魔法一样!

https://stackoverflow.com/a/5939906/4386626