CSDN博客

img Dangyuhang

用Ruby下载一个网页上的所有图片

发表于2004/10/15 9:45:00  2970人阅读

require 'net/http'
include Net

#这里是我们内网的代理服务器信息
proxy_addr = '192.168.121.241'
proxy_port = 808

#在网页源文件里搜索图片信息的正则表达式
pattern = %r{<img src/s?=/s?"([a-zA-Z0-9/._]+)"}

begin
 proxy_class = HTTP::Proxy(proxy_addr,proxy_port)
 
 proxy_class.start('www.csdn.net') {|http|
  list = Array.new
  http.get('/') do |str|
   if str =~ pattern then
    print("Get img at " + $1 + "/n")
    list.push("/" + $1)
   end   
  end
  list.each do |line|
   temp = line.split("/")
   print("Getting img:" + temp[temp.length-1])
   http.get(line) do |str|
    img_file = File.new("img/" + line.split("/")[2],"wb")
    img_file.write str
    img_file.close
   end
  end
 }
rescue Errno::EINVAL
 p "Can't open connection.",$!
end

阅读全文
0 0

相关文章推荐

img
取 消
img