Skip to content

Instantly share code, notes, and snippets.

@Meneth32
Created November 2, 2014 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Meneth32/bb0f61efa06c484cb4d0 to your computer and use it in GitHub Desktop.
Save Meneth32/bb0f61efa06c484cb4d0 to your computer and use it in GitHub Desktop.
get-imgur-album.py
# requirements:
# pip install praw
# pip install beautifulsoup4
# usage: python get-imgur-album.py <albumUrl>
# example: python get-imgur-album.py http://imgur.com/a/hCPIW
import re, praw, requests, os, glob, sys
from bs4 import BeautifulSoup
def downloadImage(imageUrl, localFileName):
response = requests.get(imageUrl)
if response.status_code == 200:
print('Downloading %s...' % (localFileName))
with open(localFileName, 'wb') as fo:
for chunk in response.iter_content(4096):
fo.write(chunk)
response.connection.close()
submissionUrl = sys.argv[1]
albumId = submissionUrl[len('http://imgur.com/a/'):]
htmlSource = requests.get(submissionUrl).text
with open('album_%s.html' % (albumId), 'wb') as fo:
fo.write(bytes(htmlSource, 'UTF-8'))
soup = BeautifulSoup(htmlSource)
matches = soup.select('.jcarousel img')
for match in matches:
imageUrl = match['data-src'].replace('s.', '.')
index = match['data-index']
if '?' in imageUrl:
imageFile = imageUrl[imageUrl.rfind('/') + 1:imageUrl.rfind('?')]
else:
imageFile = imageUrl[imageUrl.rfind('/') + 1:]
localFileName = 'album_%s_index_%s_imgur_%s' % (albumId, index, imageFile)
downloadImage('http:' + imageUrl, localFileName)
#print(localFileName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment