我构建了一个刮板来从维基百科表中提取所有信息并将其上传到我的数据库。一直都很好,直到我意识到我在图像上拉错了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