Skip to content

Instantly share code, notes, and snippets.

@peicheng
Forked from taoyu/renren.py
Created February 4, 2012 07:33
Show Gist options
  • Save peicheng/1736171 to your computer and use it in GitHub Desktop.
Save peicheng/1736171 to your computer and use it in GitHub Desktop.
download an album from renren.com
#change the two variables below as your need
#then, run python renren.py, you got it
#you also need an empty folder called "download"
data = (('email', 'test@example.com'), ('password', '123456'), ('origURL', 'http://www.renren.com/Home.do'), ('domain', "renren.com"))
album_url = 'http://photo.renren.com/photo/249794035/album-484759819'
import cookielib
import urllib2
import urllib
import re
import json
import os
import time
cookie = cookielib.CookieJar()
Opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
urllib2.install_opener(Opener)
data = urllib.urlencode(data)
print 'login...'
response = urllib2.urlopen('http://www.renren.com/PLogin.do', data).read()
print 'loading the album...'
i = 0
links = []
album_dir = 'download'
while(1):
print i
html = urllib2.urlopen(album_url+"?curpage="+str(i))
i += 1
content = html.read()
ls = re.findall(r'<a href=\"(.*)\".* class=\"img\">', content)
if len(ls)==0:
break
links += [link[:link.index('?')] for link in ls]
time.sleep(2)
for link in links:
print link
req = urllib2.Request(link + '/ajax')
request = Opener.open(req, data)
response = request.read()
photo = json.loads(response)
photo_url = photo.get('photo').get('large')
filename = re.split('\/', photo_url)[-1]
f = open(os.path.join(album_dir, filename), 'wb')
try:
photodata = urllib2.urlopen(photo_url).read()
except Exception as error:
print error
time.sleep(5)
links.append(link)
continue
f.write(photodata)
f.close()
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment