我構建了一個刮板來從維基百科表中提取所有信息並將其上傳到我的數據庫。一直都很好,直到我意識到我在圖像上拉錯了URL,並且我想要實際的圖像URL「http://upload.wikimedia.org/wikipedia/commons/thumb/3/38/Baconbutty.jpg」,而不是它容易給我的「/wiki/File:Baconbutty.jpg」。這是我到目前爲止的代碼:如何從此Wikipedia表格中提取正確的圖片網址?
def initialize
@url = "http://en.wikipedia.org/wiki/List_of_sandwiches"
@nodes = Nokogiri::HTML(open(@url))
end
def summary
sammich_data = @nodes
sammiches = sammich_data.css('div.mw-content-ltr table.wikitable tr')
sammich_data.search('sup').remove
sammich_hashes = sammiches.map {|x|
if content = x.css('td')[0]
name = content.text
end
if content = x.css('td a.image').map {|link| link ['href']}
image =content[0]
end
if content = x.css('td')[2]
origin = content.text
end
if content = x.css('td')[3]
description =content.text
end
我的問題是這一行:
if content = x.css('td a.image').map {|link| link ['href']}
image =content[0]
如果我去td a.image img
,它只是給我一個null
條目。
有什麼建議嗎?
你也在刮維基百科而不是使用它的API,這會讓你的生活更加艱難。 – 2013-05-09 17:40:01