[Python]image crawler for deviant art

#!/usr/bin/python

import urllib, re, random
from os.path import isfile
#from time import sleep
#sleep(15)

keyword="white"
page=urllib.urlopen("http://browse.deviantart.com/?order=24&q="+keyword).read()

items=re.findall('super_img="(http://.*?\.(jpg|jpeg|png))"',page)
imgs=[]
for item in items:
	item=item[0]
	#print item
	imgs.append(item)

"""
img=random.choice(imgs)
print img
content=urllib.urlopen(img).read()
fileext=img[img.rfind("."):]
filename="/home/kjwon15/scripts/img/wallpaper" #+fileext
file(filename,"w").write(content)
"""

savedfile=0
for img in imgs:
	filename=img[img.rfind("/")+1:]
	if isfile("/home/kjwon15/pictures/"+filename):
		print "file exist: "+filename
	else:
		print "save file "+filename
		contents=urllib.urlopen(img).read()
		file("/home/kjwon15/pictures/"+filename,"w").write(contents)
		savedfile+=1

print "saved "+str(savedfile)+"files"

kjwon15

I'm a hacker, I want to improve life.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.