From 19ab1b70c0fcb7fda50f4ae4e7c2c67e2d3b78a9 Mon Sep 17 00:00:00 2001 From: Leonardo Robol Date: Wed, 28 Oct 2009 08:46:17 +0100 Subject: [PATCH] Controllo del numero di link e qualche correzione di bug --- spidy.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/spidy.py b/spidy.py index 84e54db..3311c7d 100755 --- a/spidy.py +++ b/spidy.py @@ -17,7 +17,8 @@ mtx_url_dict = mutex.mutex() size = 1000 url_dict = {} -url_counter = range(size) +url_stack = range(size) +link_counter = 0 max_steps = 5 @@ -122,7 +123,7 @@ class Page(): def __get_page(self, parent): - if(len(url_counter) == 0): + if(len(url_stack) == 0): self.exhausted = True return @@ -159,7 +160,7 @@ class Page(): # Questo ci serve per tenere il # conto di tutti gli url global url_dict - global url_counter + global url_stack self.exhausted = False self.analyzed = False @@ -179,7 +180,7 @@ class Page(): else: try: - self.ID = url_counter.pop() + self.ID = url_stack.pop() except IndexError: self.exhausted = True @@ -204,7 +205,9 @@ class Page(): return 0 def __add_link(self, ID): + global link_counter url_dict[self.url].links.append(ID) + link_counter += 1 def links(self): return url_dict[self.url].links @@ -239,20 +242,25 @@ class Crawler(threading.Thread): ## dalla pagina di partenza page = Page() + ## Se ci chiedono di uscire perché abbiamo ## analizzato tutte la pagine ce ne andiamo if ExitRequired: return + ## Analogamente ce ne andiamo se non ci sono + ## più vie da seguire in questa direzione + if page.exhausted: + break + + if page.step >= max_steps: print " => Ohi, troppi step! Riparto" page = Page(self.start_page) self.waitingforpage = False - if page.exhausted: - break - + if debug >= 1: print " => Analyzing page %s" % page.url @@ -305,8 +313,8 @@ if __name__ == "__main__": debug = bool(option.debug) outfile = option.outfile size = int(option.size) - url_counter = range(size) - url_counter.reverse() + url_stack = range(size) + url_stack.reverse() max_steps = int(option.max_steps) default_page = option.start_page @@ -365,7 +373,10 @@ if __name__ == "__main__": ## Il numero massimo di pagine meno quelle avanzate = ## le pagine effettivamente usate! - out.write(str(size - len(url_counter)) + "\n") + out.write(str(size - len(url_stack)) + "\n") + + ## Il numero di elementi non 0 sono i link! + out.write(str(link_counter) + "\n") -- 2.1.4