【发布时间】:2014-06-07 00:45:14
【问题描述】:
我目前正在寻找一种访问 URL、将数据保存为 .html 文件并根据 URL 命名文件的方法。目前,我有一个 .text 文件,每一行都是一个单独的 URL:
http://www.url.com
http://www.url1.com
...
我当前的代码只是将每个 .html fi;e 保存为一个数字,但如果可能的话,我想使用 URL 本身的名称。
require 'open-uri'
n = 0
open ("urllist.txt"){|o|
while url = o.gets
url.chomp!
printf("URL: %s", url)
open(url) {|site|
fname = sprintf("html/%d.html", n)
open(fname, "w"){|out|
d=site.read
out.write(d)
}
}
n += 1
end
}
也许是使用 IO.readlines 的选项?我不知道。 谢谢。
【问题讨论】: