Ruby hpricot:从URL获取图像并解析元素
我试图得到一个网页内的图像的确切网址,然后下载它。我还没有到达下载点,因为我正在尝试隔离图像的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
#!/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]