2013-02-16 51 views
1

我正在嘗試製作Google圖片網頁上所有圖片文件的數組。如何使用Ruby和Nokogiri解析Google圖片網址?

我想"imagurl="後正則表達式來拉一切,"&amp"之前結束的看到在這個HTML:

<a href="http://www.google.com/imgres?imgurl=http://www.trendytree.com/old-world- christmas/images/20031chapel20031-silent-night-chapel.jpg&amp;imgrefurl=http://www.trendytree.com/old-world-christmas/silent-night-chapel-20031-christmas-ornament-old-world-christmas.html&amp;usg=__YJdf3xc4ydSfLQa9tYnAzavKHYQ=&amp;h=400&amp;w=400&amp;sz=58&amp;hl=en&amp;start=19&amp;zoom=1&amp;tbnid=ajDcsGGs0tgE9M:&amp;tbnh=124&amp;tbnw=124&amp;ei=qagfUbXmHKfv0QHI3oG4CQ&amp;itbs=1&amp;sa=X&amp;ved=0CE4QrQMwEg"><img height="124" width="124" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRLy5inpSdHxWuE7z3QSZw35JwN3upbBaLr11LR25noTKbSMn9-qrySSg"></a><br><cite title="trendytree.com">trendytree.com</cite><br>Silent Night Chapel <b>20031</b><br>400 × 400 - 58k - jpg</td> 

我覺得我能做到這一點與正則表達式,但我可以」找到一種方法來使用正則表達式搜索我的分析文檔,但我沒有找到任何解決方案。

回答

2
str = '<a href="http://www.google.com/imgres?imgurl=http://www.trendytree.com/old-world-  christmas/images/20031chapel20031-silent-night-chapel.jpg&amp;imgrefurl=http://www.trendytree.com/old-world-christmas/silent-night-chapel-20031-christmas-ornament-old-world-christmas.html&amp;usg=__YJdf3xc4ydSfLQa9tYnAzavKHYQ=&amp;h=400&amp;w=400&amp;sz=58&amp;hl=en&amp;start=19&amp;zoom=1&amp;tbnid=ajDcsGGs0tgE9M:&amp;tbnh=124&amp;tbnw=124&amp;ei=qagfUbXmHKfv0QHI3oG4CQ&amp;itbs=1&amp;sa=X&amp;ved=0CE4QrQMwEg"><img height="124" width="124" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRLy5inpSdHxWuE7z3QSZw35JwN3upbBaLr11LR25noTKbSMn9-qrySSg"></a><br><cite title="trendytree.com">trendytree.com</cite><br>Silent Night Chapel <b>20031</b><br>400 × 400 - 58k - jpg</td>' 
str.split('imgurl=')[1].split('&amp')[0] 
#=> "http://www.trendytree.com/old-world-  christmas/images/20031chapel20031-silent-night-chapel.jpg" 

這是你在找什麼?

1

要得到所有你想要做

# get all links 
url = 'some-google-images-url' 
links = Nokogiri::HTML(open(url)).css('a') 

# get regex match or nil on desired img 
img_urls = links.map {|a| a['href'][/imgurl=(.*?)&/, 1] } 

# get rid of nils 
img_urls.compact 

你想要的正則表達式是/imgurl=(.*?)&/因爲你要imgurl=&之間的非貪婪匹配的IMG網址,否則貪.*將採取一切到最後&在字符串中。

2

使用正則表達式的問題是您對URL中參數的順序過於瞭解。如果訂單更改,或者&amp;消失,則正則表達式將不起作用。

相反,解析URL,然後拆分值了:

# encoding: UTF-8 

require 'nokogiri' 
require 'cgi' 
require 'uri' 

doc = Nokogiri::HTML.parse('<a href="http://www.google.com/imgres?imgurl=http://www.trendytree.com/old-world-christmas/images/20031chapel20031-silent-night-chapel.jpg&amp;imgrefurl=http://www.trendytree.com/old-world-christmas/silent-night-chapel-20031-christmas-ornament-old-world-christmas.html&amp;usg=__YJdf3xc4ydSfLQa9tYnAzavKHYQ=&amp;h=400&amp;w=400&amp;sz=58&amp;hl=en&amp;start=19&amp;zoom=1&amp;tbnid=ajDcsGGs0tgE9M:&amp;tbnh=124&amp;tbnw=124&amp;ei=qagfUbXmHKfv0QHI3oG4CQ&amp;itbs=1&amp;sa=X&amp;ved=0CE4QrQMwEg"><img height="124" width="124" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRLy5inpSdHxWuE7z3QSZw35JwN3upbBaLr11LR25noTKbSMn9-qrySSg"></a><br><cite title="trendytree.com">trendytree.com</cite><br>Silent Night Chapel <b>20031</b><br>400 × 400 - 58k - jpg</td>') 

img_url = doc.search('a').each do |a| 
    query_params = CGI::parse(URI(a['href']).query) 
    puts query_params['imgurl'] 
end 

,輸出:

http://www.trendytree.com/old-world-christmas/images/20031chapel20031-silent-night-chapel.jpg 

兩個URI和CGI的使用,因爲URI的decode_www_form提高試圖解碼時異常查詢。

我也已經知道使用類似的查詢字符串轉換爲哈希解碼:

Hash[URI(a['href']).query.split('&').map{ |p| p.split('=') }] 

,將返回:

 
{"imgurl"=> 
    "http://www.trendytree.com/old-world-christmas/images/20031chapel20031-silent-night-chapel.jpg", 
"imgrefurl"=> 
    "http://www.trendytree.com/old-world-christmas/silent-night-chapel-20031-christmas-ornament-old-world-christmas.html", 
"usg"=>"__YJdf3xc4ydSfLQa9tYnAzavKHYQ", 
"h"=>"400", 
"w"=>"400", 
"sz"=>"58", 
"hl"=>"en", 
"start"=>"19", 
"zoom"=>"1", 
"tbnid"=>"ajDcsGGs0tgE9M:", 
"tbnh"=>"124", 
"tbnw"=>"124", 
"ei"=>"qagfUbXmHKfv0QHI3oG4CQ", 
"itbs"=>"1", 
"sa"=>"X", 
"ved"=>"0CE4QrQMwEg"}