Skip to content

Instantly share code, notes, and snippets.

@azizmb
Created February 24, 2012 04:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azizmb/1897613 to your computer and use it in GitHub Desktop.
Save azizmb/1897613 to your computer and use it in GitHub Desktop.
Script to download all photos from an album in imgur
from BeautifulSoup import BeautifulSoup
import requests
import os
album_url = "http://imgur.com/a/TYfWa/all/hot/day/page/%d?scrolled"
pages = 3
directory = "heroes"
posts_list = []
for page in range(0, pages):
r = requests.get(album_url%page)
soup = BeautifulSoup(r.content)
posts_list.append([d["href"] for d in soup.find("div", attrs={'class': 'posts'}).findAll("a")])
image_urls = [item for sublist in posts_list for item in sublist]
print "Found %d images."%len(image_urls)
if not os.path.exists(directory):
os.makedirs(directory)
for url in image_urls:
filename = url.split("/")[-1]
print "%s"%filename
fout = open(os.path.join(directory, filename), "wb")
fout.write(requests.get(url).raw.read())
fout.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment