1

我正在使用以下方法保存抓取的图像:

img_url = agent.page.at(".field-content a")[:href]
root_img_url = URI.join(page_url,img_url).to_s
cover = File.basename(root_img_url)
file = File.open(File.join(Rails.root, 'app', 'assets', 'images', cover), 'wb') { |f|
    f.write(open(root_img_url).read)
}

一些图像%26的名称中有一个,比如cover_b%26w_xyz_.jpg,当我将它们保存在我的数据库中并希望在我的索引视图中查看它们时,它们不会出现,但仍保存在 assets/images 文件夹中。

在将文件保存到数据库之前如何替换%26字符cover

4

1 回答 1

3

%26是 URL 编码的&,因此您可以使用URI.decoded

cover = File.basename(URI.decode(root_img_url))
# cover is now 'cover_b&w_xyz_.jpg'
于 2013-08-07T18:55:27.660 回答