python模块
一 python文章索引
二 python31代码用来自动生成本索引 (需要保存为utf-8格式)
1 #-*- coding: utf-8 -*- 2 3 import urllib.request 4 import re 5 6 pythontagurl = "http://www.cnblogs.com/itech/category/170012.html" 7 pythonarticleurlregrex = "(<a.*?href=\"http://www.cnblogs.com/itech/archive.*?>([Pp]ython.*?)</a>)" 8 9 # get the page content string which contains all python article links 10 pythontagpage = urllib.request.urlopen(pythontagurl) 11 pythontagstr = "" 12 for line in pythontagpage.readlines(): 13 try: 14 newline = line.decode('utf-8', 'strict') 15 #print(newline) 16 except: 17 continue 18 pythontagstr += newline 19 pythontagpage.close() 20 21 # get all link and sort 22 pythonlinkandtiles = re.findall(pythonarticleurlregrex, pythontagstr) 23 d = dict() 24 for link, title in pythonlinkandtiles: 25 d[title] = link 26 pythontitles = list(d.keys()) 27 bstr1 = "python基础" 28 bstr2 = "python语法" 29 estr = "python实例" 30 lstr = "python类库" 31 tstr = "python技巧" 32 ostr = "python其他" 33 basic = [] 34 examples = [] 35 libs = [] 36 tips = [] 37 others = [] 38 for k in pythontitles: 39 if k.startswith(bstr1) or k.startswith(bstr2): 40 basic.append(k) 41 elif k.startswith(estr) : 42 examples.append(k) 43 elif k.startswith(lstr) : 44 libs.append(k) 45 elif k.startswith(tstr): 46 tips.append(k) 47 else: 48 others.append(k) 49 basic.sort() 50 libs.sort() 51 examples.sort() 52 tips.sort() 53 others.sort() 54 55 pythonarticles = [] 56 fonts = "<br/><font color=red size = 5>" 57 fonte = ":</font>" 58 pythonarticles.append( fonts + bstr1 + fonte ) 59 for py in basic: pythonarticles.append(d[py]) 60 pythonarticles.append(fonts + lstr + fonte ) 61 for py in libs: pythonarticles.append(d[py]) 62 pythonarticles.append(fonts + estr + fonte ) 63 for py in examples: pythonarticles.append(d[py]) 64 pythonarticles.append(fonts + tstr + fonte ) 65 for py in tips: pythonarticles.append(d[py]) 66 pythonarticles.append(fonts + ostr + fonte ) 67 for py in others: pythonarticles.append(d[py]) 68 69 # generate pythonindex.html 70 pythonindex = open("pythonindex.html", "w",encoding='utf-8') 71 pythonindex.write("<html>") 72 pythonindex.write("<head>") 73 pythonindex.write("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>") 74 pythonindex.write("<title>Python - iTech's Blog</title>") 75 pythonindex.write("</head>") 76 pythonindex.write("<body>") 77 pythonindex.write("Total number is :" + str(len(pythonarticles)) + "</br>") 78 for pa in pythonarticles: 79 pythonindex.write(pa) 80 pythonindex.write("</br>") 81 pythonindex.write("</body>") 82 pythonindex.write("</html>") 83 pythonindex.close()