• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >ruby > Ruby中使用mechanize批量下载校内网相册照片

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

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了mechanize,python mechanize,multi mechanize,mechanize是什么意思,校内网等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

自己闲来无事,用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
</div>

</div>

您可能想查找下面的文章:

  • Ruby中使用mechanize批量下载校内网相册照片
  • Ruby中的Mechanize的使用教程

相关文章

  • 设计模式中的观察者模式在Ruby编程中的运用实例解析
  • Ruby配置rspec和RestClient来检测服务器
  • Ruby面向对象编程中类与方法的基础学习
  • 深入剖析Ruby设计模式编程中对命令模式的相关使用
  • Ruby程序中发送基于HTTP协议的请求的简单示例
  • ruby 面向对象思维 概念
  • ruby迭代map的简洁写法实现原理分析
  • Ruby中关于模块的一些基础知识
  • Ruby 中$开头的全局变量、内部变量、隐藏变量介绍
  • Ruby 字符串处理

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • ruby安装gem包失败的通用解决方法
    • rudy 重载方法 详解
    • ruby ftp封装实例详解
    • ruby 正则表达式 教程
    • Ruby on Rails框架程序连接MongoDB的教程
    • Ruby多线程编程初步入门
    • 在Ruby on Rails上使用Redis Store的方法
    • 实例解析Ruby中的数值类型以及常量
    • Ruby中的Proc类及Proc的类方法Proc.new的使用解析
    • Ruby中的数组和散列表的使用详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有