Created
April 29, 2014 00:52
-
-
Save syohex/11388201 to your computer and use it in GitHub Desktop.
simple image downloader
This file contains 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
#!/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