2009-07-24 212 views

回答

53

你很親近回答​​這個問題,我有點不願意爲你回答。但是,以下XPath應該提供你想要的(當然,提供源代碼是XHTML)。

//img[@class='photo-large']/@src 

有關更多提示,請查看W3 Schools。他們對這樣的事情有很好的教程,也是很好的參考。

9

使用角度來說,Hpricot這個工程:

doc.at('//img[@class="photo-large"]')['src'] 

在你有一個以上的圖像的情況下,下面給出一個數組:

doc.search('//img[@class="photo-large"]').map do |e| e['src'] end 

然而,引入nokogirimany times faster,它「can be used as a drop in replacement」爲Hpricot。
這裏的版本引入nokogiri,在這個XPath選擇屬性的工作原理:

doc.at('//img[@class="photo-large"]/@src').to_s 

或多幅圖像:

doc.search('//img[@class="photo-large"]/@src').to_a 
+2

磕磕絆絆就這個問題,我不禁好奇:是什麼讓你回答引用紅寶石相關depencies?對我來說,似乎相當不錯。 – 2013-12-16 14:06:52

0

// IMG/@ SRC

你可以去有了這個如果你想要一個圖像的鏈接。

例如:

<img alt="" class="avatar width-full rounded-2" height="230" src="https://avatars3.githubusercontent.com/...;s=460" width="230">