国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Ruby中使用mechanize批量下載校內網相冊照片

2019-10-26 19:25:21
字體:
來源:轉載
供稿:網友

自己閑來無事,用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}%")

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 温泉县| 东山县| 沁源县| 碌曲县| 尼玛县| 鹤庆县| 个旧市| 修水县| 崇州市| 长子县| 桂东县| 南雄市| 桂林市| 灵川县| 荣成市| 山东省| 梅州市| 内江市| 阜阳市| 麟游县| 化德县| 贵港市| 容城县| 来凤县| 泰安市| 通海县| 柘荣县| 岚皋县| 伊宁市| 岐山县| 清原| 双江| 德江县| 汝州市| 吉林省| 香格里拉县| 墨竹工卡县| 砚山县| 四川省| 四川省| 辽中县|