-
-
Save peicheng/1736171 to your computer and use it in GitHub Desktop.
download an album from renren.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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