Code: Alles auswählen
import htmllib, re, formatter, urlparse
class IMGParser(htmllib.HTMLParser):
def __init__(self, URL, verbose = 0):
htmllib.HTMLParser.__init__(self, verbose)
self.URL = URL
self.a_bilder = []
self.src_bilder = []
self.a_keine_bilder = []
def do_img(self, attrs):
for src, location in attrs:
if src == "src":
_link_url = urlparse.urlparse(location)
_temp = _link_url[2].split("/")
local_name = _temp[-1]
if _link_url[0] != "" or _link_url[1] != "":
_img_url = location
else:
_img_url = urlparse.urljoin(self.URL, _link_url[2])
_src_bild = [local_name,_img_url]
self.src_bilder.append(_src_bild)
def anchor_bgn(self, href, name, type):
_link_url = urlparse.urlparse(href) #zerlege URL zum Bild -
_temp = _link_url[2].split("/") #Trennen von Pfad wo bild auf Server liegt anhand von /
local_name = _temp[-1] #das ist der lokale name der Datei
_pattern = r"([^\\[]*).(gif|jpg|jpeg|jpe)" #Suche nach den type img gif. jpg.jpeg im localen namen
_img_true = re.match(_pattern, local_name) # "
if _img_true == None: #Wenn Link nicht zu einem bild führt
self.a_keine_bilder.append(href)
else: #sonst:
if _link_url[0] != "" or _link_url[1] != "": #Wenn URL nicht realtiv dann:
_img_url = href #href ist die fertige Adresse zum Bild
else: #sonst:
_img_url = urlparse.urljoin(self.URL, _link_url[2]) #Adresse erstellen
_a_bild = [local_name, _img_url]
self.a_bilder.append(_a_bild)
#url = "http://bilder.de/index.html"
#_host = urllib.urlopen(url)
#_h = IMGParser(url, formatter.NullFormatter(None))
#_h.feed(_host.read())
#print _h.a_keine_bilder
#print "\n"
#print _h.a_bilder
#print "\n"
#print _h.src_bilder
Andreas