Lista Hyperlink
Programma che stampa a video una lista dei link contenuti in una pagina web (realizzato da Markon). Il modulo BeautifulSoup è reperibile qui
1 import BeautifulSoup
2 import urllib
3 import re
4
5 sito = urllib.urlopen("http://www.google.it") # apriamo l'indirizzo www.google.it
6 parsing = BeautifulSoup.BeautifulSoup(sito) # eseguiamo il parsing della pagina
7 cerca = parsing.findAll("a",{"href":re.compile("^.*")}) # cerchiamo tutti gli <a href="...">
8 sitistringhe= {}
9
10 for i in cerca:
11 sitistringhe[i.string] = i['href'] # creiamo un dizionario chiave-valore dove le chiavi
12 # sono le stringhe che vediamo nel browser, e i valori i link
13
14 for key in sitistringhe:
15 print key, "\t", sitistringhe[key] # le stampiamo
Orologio binario
Stampa l'ora in formato binario utilizzando le librerie curses
1 #!/usr/bin/python
2
3 # Orologio binario
4
5 import curses
6 from time import gmtime, strftime
7
8 def dec2bin(new_dec):
9 bin = ""
10 while (new_dec != 0):
11 bin = str(new_dec%2) + bin
12 new_dec = new_dec/2
13 return bin.zfill(6)
14
15 def getScreen():
16 stdscr = curses.initscr()
17 curses.nl()
18 curses.echo()
19 curses.curs_set(0)
20 curses.newwin(0, 0)
21 return stdscr
22
23 def bynaryClock():
24 while 1:
25 try:
26 stdscr = getScreen()
27 ora = strftime("%H:%M:%S", gmtime())
28 nuova_ora = ':'.join( str(dec2bin(int(i))) for i in ora.split(':') )
29
30 BODY = """
31 < BINARY PYCLOCK >
32 ____________________
33 | |
34 |%s|
35 |____________________|""" % nuova_ora
36
37 stdscr.addstr(0, 0, BODY)
38 stdscr.refresh()
39
40 except KeyboardInterrupt:
41 curses.endwin()
42 break
43
44 if __name__ == "__main__":
45 bynaryClock()