Chinaunix

标题: 使用mechanize分析并批量下载校内网相册照片 [打印本页]

作者: life-boy    时间: 2011-01-14 12:54
标题: 使用mechanize分析并批量下载校内网相册照片
自己闲来无事,用mechanize做了一个可以下载校内相册照片的代码
写的有些简陋。。。。主要是根据相册的地址来分析用户ID和相册ID,然后模拟请求相册页面,并提取所有照片,然后下载到本地的一个文件夹中
ruby版本:ruby1.8.7 or ruby1.9.2
操作系统:windows 7
linux下没有试过~不过应该也可以的
  1. #encoding: utf-8
  2. require 'rubygems'
  3. require 'mechanize'

  4. class Renren

  5.   def initialize(e,p)
  6.     @agent = Mechanize.new
  7.     @page = @agent.get('http://3g.renren.com/')
  8.     @page = @page.form_with(:method => 'POST') do |r|
  9.       r.email = e
  10.       r.password = p
  11.     end.submit
  12.   end

  13.   def fetch_other_photo(album_link,foldername)
  14.     photo_urls = []
  15.     puts Iconv.conv("gb2312", "utf-8", "开始分析相册地址.....")
  16.     begin
  17.       user_id,album_id = parse_album_uri(album_link)
  18.     rescue
  19.       puts Iconv.conv("gb2312", "utf-8", "您的相册地址不正确,请重新输入!")
  20.       return
  21.     end
  22.     page = @agent.get("http://3g.renren.com/album/wgetalbum.do?id=#{user_id}&owner=#{album_id}")
  23.     puts Iconv.conv("gb2312", "utf-8", "正在获取所有照片地址.....")
  24.     loop do
  25.       page.links_with(:href => /http:\/\/3g\.renren\.com\/album\/wgetphoto\.do?/).each do |link|
  26.         photo = link.click
  27.         photo_urls << photo.link_with(:text => "下载该图").href
  28.       end
  29.       break if page.link_with(:text => "下一页").nil?
  30.       page = page.link_with(:text => "下一页").click
  31.     end
  32.     if photo_urls.length > 0
  33.       puts Iconv.conv("gb2312", "utf-8", "开始下载相册.....")
  34.       unless File.directory?("#{foldername}")
  35.         Dir.mkdir("#{foldername}")
  36.       end
  37.       Dir.chdir("#{foldername}") do |path|
  38.           photo_urls.each do |photo_url|
  39.             @agent.get(photo_url) do |photo|
  40.               puts Iconv.conv("gb2312","utf-8","正在保存文件#{photo.filename}……已经下载#{((photo_urls.index(photo_url)+1).to_f/photo_urls.length*100).to_i}%")
  41.               photo.save
  42.             end
  43.           end
  44.       end
  45.       puts Iconv.conv("gb2312","utf-8","相册下载完毕.....")
  46.     else
  47.       puts Iconv.conv("gb2312","utf-8","相册内没有照片哟~")
  48.     end
  49.   end

  50.   private

  51.   def parse_album_uri(uri)
  52.     uri = uri.chomp("#thumb")
  53.     uri = uri.split("?")
  54.     if uri.length > 1 and uri[1].include?("owner")
  55.       uri = uri[1].split("&")
  56.       user_id = uri[0].split("=")[1]
  57.       album_id = uri[1].split("=")[1]
  58.     else
  59.       uri = uri[0].split("/")
  60.       album_id = uri[4]
  61.       user_id = uri[5].split("-")[1]
  62.     end
  63.     return user_id,album_id
  64.   end
  65. end

  66. print Iconv.conv("gb2312","utf-8","用户名:")
  67. username = gets.chomp()
  68. print Iconv.conv("gb2312","utf-8","密码:")
  69. password = gets.chomp()
  70. renren = Renren.new(username,password)
  71. loop do
  72.   print Iconv.conv("gb2312","utf-8","粘贴相册地址:")
  73.   uri = gets.chomp()
  74.   renren.fetch_other_photo(uri, username)
  75.   print Iconv.conv("gb2312","utf-8","按0退出程序,按其它键继续下载其它相册:")
  76.   break if gets.chomp() == "0"
  77. end
复制代码

作者: tti    时间: 2011-01-18 11:16
C:\>ruby -v
ruby 1.9.2p136 (2010-12-25) [i386-mingw32]

复制你的代码 到a.rb 出错 如下:
C:/a.rb:18: invalid multibyte char (UTF-
C:/a.rb:18: invalid multibyte char (UTF-
C:/a.rb:18: syntax error, unexpected $end, expecting ')'
    puts Iconv.conv("gb2312", "utf-8", "开始分析相册地址....."
作者: 2gua    时间: 2011-01-18 16:29
LS,请保存你的文件为GBK格式吧。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2