自己闲来无事,用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}%")
新闻热点
疑难解答