Questo codice scansiona alcuni documenti di esempio e li scrive nella console, strutturati ad albero:
from elementtree.ElementTree import Element from elementtree.ElementTree import parse import urllib proxies = {'http': 'http://proxy:80'} def scanNode(node, depth): for element in node.getchildren(): if element.text and element.text.strip(): print " "*depth + element.tag, "(" + element.text + ")" else: print " "*depth + element.tag if element.getchildren(): scanNode(element, depth+1) def parseTree(url): print "--] Parsing di " + url + " [--" tree = parse(urllib.urlopen(url, proxies=proxies)) print tree.getroot().tag scanNode(tree.getroot(), 2) print def main(): parseTree("http://www.w3schools.com/xml/note.xml") parseTree("http://www.w3schools.com/xml/cd_catalog.xml") parseTree("http://www.w3schools.com/xml/plant_catalog.xml") parseTree("http://www.w3schools.com/xml/simple.xml") if __name__ == "__main__": main()
Semplice semplice, ma lo scrivo qui perché mi può sempre servire!
Nessun commento:
Posta un commento