import re
import sys
import string
from urllib import *
import urlparse
if len(sys.argv) < 2:
print "usage: <python> ", sys.argv[0], " <url>"
sys.exit()
# define some constants
LINKDEPTH = 2 # don't go depth first into links more than 5 times or else it locks up
QSIGNAL = 0 # if QSIGNAL == 1 that means emergency stop
linkre = r"a[^h]+href[ \t]*=[ \t]*[\'\"]?([^\'\"> ]*)[\'\"]?[ \t]*"
imgre = r"img[^s]+src[ \t]*=[ \t]*[\'\"]?([^\'\"> ]*)[\'\"]?[ \t]*"
# compile the regular expressions
locatel = re.compile(linkre)
locatei = re.compile(imgre)
def init():
return file("output.txt", "w")
def cleanup(f):
f.write(".\n")
f.close()
def locate(fn, d, f):
global LINKDEPTH
# don't go over the recursion number
if d == LINKDEPTH:
return
try:
# opening some random URL has a good probability of failing, so..
fi = urlopen(fn)
tomatch = string.lower(fi.read())
fi.close()
except:
# if it fails with an exception, just get out of there
return
# find all the links on the webpage
ml = locatel.findall(tomatch)
# find all the images on the webpage
mi = locatei.findall(tomatch)
f.write("\nlinks found at " + fn + " --------------+\n")
for x in ml:
um = urlparse.urlparse(x)[2]
if um[len(um)-4:] == '.jpg':
f.write("! " + urlparse.urljoin(fn, x) + "\n")
print "going to " + x
locate(x, d+1, f)
print "returning from " + x
f.write("\nimages found at " + fn + " --------------+\n")
for x in mi:
f.write("! " + urlparse.urljoin(fn, x) + "\n")
fl = init()
print "going to " + sys.argv[1]
locate(sys.argv[1], 0, fl)
cleanup(fl)