主页 > 知识库 > Ruby中使用mechanize批量下载校内网相册照片

Ruby中使用mechanize批量下载校内网相册照片

热门标签:电销机器人厂商代理 高德地图标注客服 徐州网络外呼系统哪个好 常德电销平台外呼系统软件价格 百度地图标注自定义图片 湖州u友防封电销卡 白银外呼paas系统 滴滴外呼系统 地图标注赚钱项目注册

自己闲来无事,用mechanize做了一个可以下载校内相册照片的代码。

写的有些简陋。。。。主要是根据相册的地址来分析用户ID和相册ID,然后模拟请求相册页面,并提取所有照片,然后下载到本地的一个文件夹中。

ruby版本:ruby1.8.7 or ruby1.9.2
操作系统:windows 7

Linux下没有试过~不过应该也可以的:

复制代码 代码如下:

#encoding: utf-8
require 'rubygems'
require 'mechanize'

class Renren

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

  def fetch_other_photo(album_link,foldername)
    photo_urls = []
    puts Iconv.conv("gb2312", "utf-8", "开始分析相册地址.....")
    begin
      user_id,album_id = parse_album_uri(album_link)
    rescue
      puts Iconv.conv("gb2312", "utf-8", "您的相册地址不正确,请重新输入!")
      return
    end
    page = @agent.get("http://3g.renren.com/album/wgetalbum.do?id=#{user_id}owner=#{album_id}")
    puts Iconv.conv("gb2312", "utf-8", "正在获取所有照片地址.....")
    loop do
      page.links_with(:href => /http:\/\/3g\.renren\.com\/album\/wgetphoto\.do?/).each do |link|
        photo = link.click
        photo_urls photo.link_with(:text => "下载该图").href
      end
      break if page.link_with(:text => "下一页").nil?
      page = page.link_with(:text => "下一页").click
    end
    if photo_urls.length > 0
      puts Iconv.conv("gb2312", "utf-8", "开始下载相册.....")
      unless File.directory?("#{foldername}")
        Dir.mkdir("#{foldername}")
      end
      Dir.chdir("#{foldername}") do |path|
          photo_urls.each do |photo_url|
            @agent.get(photo_url) do |photo|
              puts Iconv.conv("gb2312","utf-8","正在保存文件#{photo.filename}……已经下载#{((photo_urls.index(photo_url)+1).to_f/photo_urls.length*100).to_i}%")
              photo.save
            end
          end
      end
      puts Iconv.conv("gb2312","utf-8","相册下载完毕.....")
    else
      puts Iconv.conv("gb2312","utf-8","相册内没有照片哟~")
    end
  end

  private

  def parse_album_uri(uri)
    uri = uri.chomp("#thumb")
    uri = uri.split("?")
    if uri.length > 1 and uri[1].include?("owner")
      uri = uri[1].split("")
      user_id = uri[0].split("=")[1]
      album_id = uri[1].split("=")[1]
    else
      uri = uri[0].split("/")
      album_id = uri[4]
      user_id = uri[5].split("-")[1]
    end
    return user_id,album_id
  end
end

print Iconv.conv("gb2312","utf-8","用户名:")
username = gets.chomp()
print Iconv.conv("gb2312","utf-8","密码:")
password = gets.chomp()
renren = Renren.new(username,password)
loop do
  print Iconv.conv("gb2312","utf-8","粘贴相册地址:")
  uri = gets.chomp()
  renren.fetch_other_photo(uri, username)
  print Iconv.conv("gb2312","utf-8","按0退出程序,按其它键继续下载其它相册:")
  break if gets.chomp() == "0"
end

您可能感兴趣的文章:
  • Ruby中使用多线程队列(Queue)实现下载博客文章保存到本地文件
  • ruby实现的一个异步文件下载HttpServer实例
  • 比较不错的关于ruby的电子书下载地址集合
  • windows和linux下Ruby的下载与安装
  • Ruby使用eventmachine为HTTP服务器添加文件下载功能

标签:辽宁 梧州 普洱 荆门 张家界 公主岭 永州 三沙

巨人网络通讯声明:本文标题《Ruby中使用mechanize批量下载校内网相册照片》,本文关键词  Ruby,中,使用,mechanize,批量,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《Ruby中使用mechanize批量下载校内网相册照片》相关的同类信息!
  • 本页收集关于Ruby中使用mechanize批量下载校内网相册照片的相关信息资讯供网民参考!
  • 推荐文章