python学习笔记(22)--漫画生成html最终版
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习笔记(22)--漫画生成html最终版相关的知识,希望对你有一定的参考价值。
说明(2017.3.14):
1. 在主文件夹生成一个main.html作为目录
2. 在每个子文件夹生成一个index.html作为看图网页
3. 通过python批量生成html网页,js配合进行遍历输出a链接
4. 生成一个booklist.js文件,存放目录数组,不然字符串不能与数组连接。这个数组是通过字符串伪装出来的,遍历数组,每个元素加上引号和逗号,去掉最后一个逗号,最后两边加上括号,var赋值就可以了。
5. 效果还不错,可以作为本地小黄网了!
1 #!/usr/bin/python 2 # -*- coding: utf-8 -*- 3 import os 4 5 # pathMain = r"E:\manhua\mh" 6 pathMain = os.getcwd() 7 def createIndex(): 8 for paths, folders, filenames in os.walk(pathMain): 9 # print(folders) 10 for folder in folders: 11 root = os.path.join(paths,folder) 12 page = len(os.listdir(root)) 13 # print(page) 14 os.chdir(root) 15 html = open("index.html","w",encoding="utf-8") 16 text = ‘‘‘ 17 <!DOCTYPE html> 18 <html lang="en"> 19 <head> 20 <meta charset="UTF-8"> 21 <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" /> 22 <title>‘‘‘+folder+‘‘‘</title> 23 <style type="text/css"> 24 body{ 25 text-align: center; 26 } 27 img{ 28 width: 80%; 29 } 30 </style> 31 </head> 32 <body> 33 <h1>‘‘‘+folder+‘‘‘</h1> 34 <div id="div"> 35 </div> 36 </body> 37 <script type="text/javascript"> 38 var page = ‘‘‘+str(page)+‘‘‘; 39 var text = ""; 40 for (var i = 0; i < page; i++) { 41 text += ‘<img src="‘+i+‘.jpg" >‘; 42 } 43 var div = document.getElementById("div"); 44 div.innerHTML = text; 45 </script> 46 </html> 47 ‘‘‘ 48 html.write(text) 49 html.close() 50 def createMain(): 51 filenames = os.listdir(pathMain) 52 os.chdir(pathMain) 53 folders = [] 54 for filename in filenames: 55 if os.path.isdir(filename): 56 folders.append(filename) 57 58 with open("booklist.js","w",encoding="utf-8") as f1: 59 txt1 = "" 60 for folder in folders: 61 txt1 += "‘"+folder+"‘" + "," 62 booklist = ‘‘‘ 63 var booklist = [‘‘‘+txt1[0:-1]+‘‘‘]; 64 ‘‘‘ 65 f1.write(booklist) 66 with open("main.html","w",encoding="utf-8") as f: 67 txt = ‘‘‘ 68 <!DOCTYPE html> 69 <html lang="en"> 70 <head> 71 <meta charset="UTF-8"> 72 <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" /> 73 <title>妖气漫画</title> 74 <style type="text/css"> 75 body{ 76 font-family: "Microsoft YaHei"; 77 font-size: 16px; 78 } 79 </style> 80 </head> 81 <body> 82 <div id="div"> 83 </div> 84 </body> 85 <script src="booklist.js"></script> 86 <script type="text/javascript"> 87 var folders = booklist; 88 var div = document.getElementById("div"); 89 var txt = ""; 90 for (var i = 0; i < folders.length; i++) { 91 txt += ‘<a href="‘+folders[i]+‘/index.html">‘+folders[i]+‘</a><br/>‘; 92 } 93 div.innerHTML = txt; 94 </script> 95 </html> 96 97 ‘‘‘ 98 f.write(txt) 99 100 createMain() 101 createIndex()
以上是关于python学习笔记(22)--漫画生成html最终版的主要内容,如果未能解决你的问题,请参考以下文章
史上最通俗易懂的Python入门教程,漫画图文生动有趣,一看就会