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

首頁 > 編程 > Ruby > 正文

Ruby中使用mechanize批量下載照片的方法

2020-02-24 15:38:18
字體:
來源:轉載
供稿:網友

method_missing是Ruby元編程中的常見技術,基本思想是通過實現方法來回調的,本文是武林技術頻道小編和大家分享的Ruby中使用mechanize批量下載照片的方法,希望對你學習有幫助!

復制代碼 代碼如下:

#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中使用mechanize批量下載照片的方法,不過在實際操作的時候,程序員一定要謹慎,選擇武林技術頻道網進行學習,會讓您受益匪淺哦!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 洛宁县| 勃利县| 礼泉县| 合水县| 宿州市| 自贡市| 长治市| 武陟县| 都江堰市| 东乌珠穆沁旗| 抚顺市| 五大连池市| 曲水县| 嘉义县| 富蕴县| 新昌县| 乌海市| 庆安县| 娱乐| 阳新县| 九江市| 高碑店市| 平昌县| 峨边| 七台河市| 剑阁县| 萨迦县| 宁乡县| 三亚市| 岐山县| 通山县| 舞阳县| 全南县| 外汇| 西青区| 新津县| 任丘市| 驻马店市| 独山县| 平山县| 双辽市|