Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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 hpricot:从URL获取图像并解析元素_Ruby_Regex_Hpricot - Fatal编程技术网

Ruby hpricot:从URL获取图像并解析元素

Ruby hpricot:从URL获取图像并解析元素,ruby,regex,hpricot,Ruby,Regex,Hpricot,我试图得到一个网页内的图像的确切网址,然后下载它。我还没有到达下载点,因为我正在尝试隔离图像的URL。代码如下: #!/usr/bin/ruby -w require 'rubygems' require 'hpricot' require 'open-uri' raw = Hpricot(open("http://www.amazon.com/Weezer/dp/B000003TAW/")) ele = raw.search("img[@src*=jpg]").first img = el

我试图得到一个网页内的图像的确切网址,然后下载它。我还没有到达下载点,因为我正在尝试隔离图像的URL。代码如下:

#!/usr/bin/ruby -w

require 'rubygems'
require 'hpricot'
require 'open-uri'

raw = Hpricot(open("http://www.amazon.com/Weezer/dp/B000003TAW/"))
ele = raw.search("img[@src*=jpg]").first
img = ele.match("(\")(.*?)(\")").captures
puts img[1]
当我按原样运行时,我收到:

undefined method `match' for #<Hpricot::Elem:0xb731948c> (NoMethodError)
我得到:

<img src="http://ecx.images-amazon.com/images/I/51rpVNqXmYL._SL500_AA240_.jpg" style="display:none;" />
这就回来了

undefined method `match' for nil:NilClass (NoMethodError)
我迷路了。请原谅我的无知,因为我刚刚开始学习ruby。感谢您的帮助

更改此行:

img = ele.match("(\")(.*?)(\")").captures 而你得到的是虚假的

但是,您可以:

ele.to_s.match("(\")(.*?)(\")").captures[1] 元素匹配(“(\”)(*?(\”)。捕获[1]
秘密就在
to_

中,如果您想调试您的对象,请尝试
放置img.inspect
而不是
放置img
,因为
将img
调用
打印img.to_;打印“\n”
perfect,效果很好。我曾试着走这条路,但我试着用绳子,如果研究得更好的话,我本可以弄清楚的。@Anton谢谢。看起来像。inspect是一个非常有价值的测试工具。以前从未使用过,但将来肯定会使用。 img = ele.match("(\")(.*?)(\")").captures img = ele[:src] ele.responde.to? :match ele.to_s.match("(\")(.*?)(\")").captures[1]