Ruby uses mechanize to bulk download Facebook photo _ruby topics

Source: Internet
Author: User

I have nothing to do, with Mechanize do a can download the campus photo album Code.

It's a little shabby .... The main is based on the address of the album to analyze the user ID and photo album ID, and then simulate the request album page, and extract all the photos, and then download to a local folder.

Ruby version: ruby1.8.7 or ruby1.9.2
Operating system: Windows 7

Linux has not tried ~ but should also be able to:

Copy Code code as follows:

#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", "Start analyzing album Address ...")
Begin
user_id,album_id = Parse_album_uri (Album_link)
Rescue
Puts Iconv.conv ("gb2312", "Utf-8", "Your album address is incorrect, please re-enter!") ")
Return
End
page = @agent. Get ("http://3g.renren.com/album/wgetalbum.do?id=#{user_id}&owner=#{album_id}")
Puts Iconv.conv ("gb2312", "Utf-8", "Getting all photo addresses ...")
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 => "Download the map"). href
End
Break If Page.link_with (: Text => "Next page"). Nil?
page = Page.link_with (: Text => "Next page"). Click
End
If photo_urls.length > 0
Puts Iconv.conv ("gb2312", "Utf-8", "Start downloading albums ...")
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", "Saving file #{photo.filename} ... #{has been downloaded (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", "album Download Complete ...")
Else
Puts Iconv.conv ("gb2312", "Utf-8", "no photos in the album Yo ~")
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", "User name:")
Username = Gets.chomp ()
Print Iconv.conv ("gb2312", "Utf-8", "Password:")
Password = Gets.chomp ()
Renren = Renren.new (Username,password)
Loop do
Print Iconv.conv ("gb2312", "Utf-8", "Paste album Address:")
URI = Gets.chomp ()
Renren.fetch_other_photo (URI, username)
Print Iconv.conv ("gb2312", "Utf-8", "Press 0 to exit the program, press the other key to continue downloading other albums:")
Break if gets.chomp () = = "0"
End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.