Skip to content

Instantly share code, notes, and snippets.

@syohex
Created April 29, 2014 00:52
Show Gist options
  • Save syohex/11388201 to your computer and use it in GitHub Desktop.
Save syohex/11388201 to your computer and use it in GitHub Desktop.
simple image downloader
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import re
import requests
from bs4 import BeautifulSoup
if len(sys.argv) < 2:
print("Usage: scrape.py url")
os.exit(0)
url = sys.argv[1]
r = requests.get(url)
soup = BeautifulSoup(r.text)
index = 0
imgs = soup.findAll('a')
for img in imgs:
src = img.get('href')
match = re.search('\.jpe?g$', src)
if match is None:
continue
output = str(index) + os.path.basename(src)
index += 1
with open(output, mode="wb") as f:
print("Download: {}".format(src))
content = requests.get(src)
if content.status_code == 200:
f.write(content.content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment