Skip to content

Instantly share code, notes, and snippets.

@ialhashim
Last active August 29, 2015 14:02
Show Gist options
  • Save ialhashim/f04f3afd78edfede3a1c to your computer and use it in GitHub Desktop.
Save ialhashim/f04f3afd78edfede3a1c to your computer and use it in GitHub Desktop.
from random import sample
from string import digits, ascii_uppercase, ascii_lowercase
from tempfile import gettempdir
from os import path
def rand_fname(suffix='', length=8):
chars = ascii_lowercase + ascii_uppercase + digits
#fname = path.join(gettempdir(), 'tmp-' + ''.join(sample(chars, length)) + suffix)
fname = 'tmp-' + ''.join(sample(chars, length)) + suffix
return fname if not path.exists(fname) \
else rand_fname(suffix, length)
import os
import re
import urllib
import urllib.request
def getimages(html_file):
linestring = open(html_file, encoding="utf8").read()
for match in re.findall(r'imgurl=(.*?(?:&|\.(?:jpg|gif|png|jpeg)))', linestring, re.I):
#print (match)
file_save_dir = 'images'
filename_length = 20
filename = rand_fname()
try:
dest = os.path.join(file_save_dir, filename + '.png')
print ("OK, saving to:" + dest)
urllib.request.urlretrieve (match, dest)
except Exception as e:
print ("Error: %s" % e)
import sys
if len(sys.argv) > 1:
getimages( sys.argv[1] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment