佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Ruby中使用mechanize批量下載校內(nèi)網(wǎng)相冊照片

Ruby中使用mechanize批量下載校內(nèi)網(wǎng)相冊照片

熱門標(biāo)簽:語音系統(tǒng) Win7旗艦版 客戶服務(wù) 百度AI接口 電話運營中心 硅谷的囚徒呼叫中心 企業(yè)做大做強 呼叫中心市場需求

自己閑來無事,用mechanize做了一個可以下載校內(nèi)相冊照片的代碼。

寫的有些簡陋。。。。主要是根據(jù)相冊的地址來分析用戶ID和相冊ID,然后模擬請求相冊頁面,并提取所有照片,然后下載到本地的一個文件夾中。

ruby版本:ruby1.8.7 or ruby1.9.2
操作系統(tǒng):windows 7

Linux下沒有試過~不過應(yīng)該也可以的:

復(fù)制代碼 代碼如下:

#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}……已經(jīng)下載#{((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","相冊內(nèi)沒有照片喲~")
    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退出程序,按其它鍵繼續(xù)下載其它相冊:")
  break if gets.chomp() == "0"
end

您可能感興趣的文章:
  • Ruby中使用多線程隊列(Queue)實現(xiàn)下載博客文章保存到本地文件
  • ruby實現(xiàn)的一個異步文件下載HttpServer實例
  • 比較不錯的關(guān)于ruby的電子書下載地址集合
  • windows和linux下Ruby的下載與安裝
  • Ruby使用eventmachine為HTTP服務(wù)器添加文件下載功能

標(biāo)簽:長沙 喀什 山西 安康 山西 海南 崇左 濟南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ruby中使用mechanize批量下載校內(nèi)網(wǎng)相冊照片》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    桦川县| 远安县| 青冈县| 秦安县| 花莲县| 千阳县| 辰溪县| 林口县| 隆林| 昂仁县| 荔浦县| 莲花县| 防城港市| 陈巴尔虎旗| 华亭县| 淮安市| 衡山县| 兴业县| 甘孜| 正镶白旗| 璧山县| 贺兰县| 咸阳市| 额敏县| 焉耆| 潞城市| 珲春市| 望江县| 灵寿县| 开远市| 德江县| 荆州市| 昭苏县| 桐乡市| 沙湾县| 资中县| 托克托县| 西藏| 彰武县| 宁安市| 岳西县|