[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创代码] python关键词替换模板生成网页.py

  1. """
  2. python关键词替换模板生成网页.py
  3. http://www.bathome.net/thread-37777-1-1.html
  4. 依山居 1:07 2015/11/24
  5. →_→强烈鄙视做垃圾站的
  6. """
  7. import random
  8. import os
  9. with open("host.txt",encoding="utf-8") as f:
  10.     host=[r.rstrip() for r in f.readlines()]
  11. with open("key.txt",encoding="utf-8") as f:
  12.     key=[r.rstrip() for r in f.readlines()]
  13. with open("moban.txt",encoding="utf-8") as f:
  14.     moban=f.read()
  15. for h in host:
  16.     rkey=random.sample(key,4)
  17.     dirs="./"+h+"/"+"www."+h+"/"
  18.     html=moban
  19.     #一行代码长到天涯海角~
  20.     html=html.replace("{关键词1}",rkey[0]).replace("{关键词2}",rkey[1]).replace("{关键词3}",rkey[2]).replace("{关键词4}",rkey[3])
  21.     if not os.path.exists(dirs):
  22.         os.makedirs(dirs)
  23.     with open(dirs+"index.htm","w+",encoding="utf-8") as f:
  24.         f.write(html)
复制代码
1

评分人数

    • CrLf: 感谢分享技术 + 1
下载安装python3 https://www.python.org/downloads/ 代码存为xx.py 双击运行或IDLE打开F5运行

  1. """
  2. python format替换模板文件中的字符串.py
  3. 问题来源 http://www.bathome.net/thread-37777-1-1.html
  4. 几天不写代码,手生...
  5. 今天路上看到format和%s替换模板文件的用法。写出来巩固一下。
  6. """
  7. import random
  8. import os
  9. #先把模板中需要替换的地方改成:{0[0]}这样的格式,可以直接用format直接替代。
  10. moban="""
  11. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  12. <html xmlns="http://www.w3.org/1999/xhtml">
  13. <head>
  14. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  15. <title>{0[0]}{0[1]}{0[2]}{0[3]}</title>
  16. <meta name="keywords" content="{0[0]}{0[1]}{0[2]}{0[3]}" />
  17. <meta name="description" content="{0[0]}努力打造{0[1]}影视导航系统为最好的{0[3]}影视系统!" />
  18. </head>
  19. 上下标签比如关键词1要一样。
  20. <li>Copyright © 2015 {0[0]} all rights reserved.  <a href="/detail/map.html" target="_blank">网站地图</a></li>
  21. </body>
  22. </html>
  23. """
  24. with open("host.txt",encoding="utf-8") as f:
  25.     host=[r.rstrip() for r in f.readlines()]
  26. with open("key.txt",encoding="utf-8") as f:
  27.     key=[r.rstrip() for r in f.readlines()]
  28. for h in host:
  29.     rkey=random.sample(key,4)
  30.     dirs="./"+h+"/"+"www."+h+"/"
  31.     if not os.path.exists(dirs):
  32.         os.makedirs(dirs)
  33.     with open(dirs+"index.html","w+",encoding="utf-8") as f:
  34.         f.write(moban.format(rkey))
复制代码
下载安装python3 https://www.python.org/downloads/ 代码存为xx.py 双击运行或IDLE打开F5运行

TOP

  1. """
  2. python format替换模板文件中的字符串2.py
  3. 问题来源 http://www.bathome.net/thread-37777-1-1.html
  4. 几天不写代码,手生...
  5. 今天路上看到format和%s替换模板文件的用法。写出来巩固一下。
  6. 再来一个format替换的写法。模板文件中的格式为{关键词1},加上python3支持中文变量名,
  7. 所以可format(关键词1='xx')的方式替换.
  8. """
  9. import random
  10. import os
  11. with open("host.txt",encoding="utf-8") as f:
  12.     host=[r.rstrip() for r in f.readlines()]
  13. with open("key.txt",encoding="utf-8") as f:
  14.     key=[r.rstrip() for r in f.readlines()]
  15. with open("moban.txt",encoding="utf-8") as f:
  16.     moban=f.read()
  17. for h in host:
  18.     rkey=random.sample(key,4)
  19.     dirs="./"+h+"/"+"www."+h+"/"
  20.     if not os.path.exists(dirs):
  21.         os.makedirs(dirs)
  22.     with open(dirs+"index.html","w+",encoding="utf-8") as f:
  23.         f.write(moban.format(关键词1=rkey[0],关键词2=rkey[1],关键词3=rkey[2],关键词4=rkey[3]))
复制代码
下载安装python3 https://www.python.org/downloads/ 代码存为xx.py 双击运行或IDLE打开F5运行

TOP

  1. """
  2. python format替换模板文件中的字符串3.py
  3. 问题来源 http://www.bathome.net/thread-37777-1-1.html
  4. 几天不写代码,手生...
  5. 今天路上看到format和%s替换模板文件的用法。写出来巩固一下。
  6. """
  7. import random
  8. import os
  9. #先把模板中需要替换的地方改成:{关键词[0]}这样的格式,就可以直接用format直接替换。
  10. moban="""
  11. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  12. <html xmlns="http://www.w3.org/1999/xhtml">
  13. <head>
  14. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  15. <title>{关键词[0]}{关键词[1]}{关键词[2]}{关键词[3]}</title>
  16. <meta name="keywords" content="{关键词[0]}{关键词[1]}{关键词[2]}{关键词[3]}" />
  17. <meta name="description" content="{关键词[0]}努力打造{关键词[1]}影视导航系统为最好的{关键词[3]}影视系统!" />
  18. </head>
  19. 上下标签比如关键词1要一样。
  20. <li>Copyright © 2015 {关键词[0]} all rights reserved.  <a href="/detail/map.html" target="_blank">网站地图</a></li>
  21. </body>
  22. </html>
  23. """
  24. with open("host.txt",encoding="utf-8") as f:
  25.     host=[r.rstrip() for r in f.readlines()]
  26. with open("key.txt",encoding="utf-8") as f:
  27.     key=[r.rstrip() for r in f.readlines()]
  28. for h in host:
  29.     dirs="./"+h+"/"+"www."+h+"/"
  30.     if not os.path.exists(dirs):
  31.         os.makedirs(dirs)
  32.     with open(dirs+"index.html","w+",encoding="utf-8") as f:
  33.         f.write(moban.format(关键词=random.sample(key,4)))
复制代码
下载安装python3 https://www.python.org/downloads/ 代码存为xx.py 双击运行或IDLE打开F5运行

TOP

几个代码方法略有不同。功能效果是相同的。
  1. """
  2. python %操作符替换模板文件中的字符串.py
  3. 6:04 2015/12/10
  4. 问题来源 http://www.bathome.net/thread-37777-1-1.html
  5. 几天不写代码,手生...
  6. 今天路上看到format和%s替换模板文件的用法。写出来巩固一下。
  7. """
  8. import random
  9. import os
  10. #先把模板中需要替换的地方改成:%(关键词1)s这样的格式
  11. moban="""
  12. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  13. <html xmlns="http://www.w3.org/1999/xhtml">
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  16. <title>%(关键词1)s%(关键词2)s%(关键词3)s%(关键词4)s</title>
  17. <meta name="keywords" content="%(关键词1)s%(关键词2)s%(关键词3)s%(关键词4)s" />
  18. <meta name="description" content="%(关键词1)s努力打造%(关键词2)s影视导航系统为最好的%(关键词3)s影视系统!" />
  19. </head>
  20. 上下标签比如关键词1要一样。
  21. <li>Copyright © 2015 %(关键词1)s all rights reserved.  <a href="/detail/map.html" target="_blank">网站地图</a></li>
  22. </body>
  23. </html>
  24. """
  25. with open("host.txt",encoding="utf-8") as f:
  26.     host=[r.rstrip() for r in f.readlines()]
  27. with open("key.txt",encoding="utf-8") as f:
  28.     key=[r.rstrip() for r in f.readlines()]
  29. for h in host:
  30.     rkey=random.sample(key,4)
  31.     关键词={"关键词"+str(r+1):rkey[r] for r in range(len(rkey))}
  32.     dirs="./"+h+"/"+"www."+h+"/"
  33.     if not os.path.exists(dirs):
  34.         os.makedirs(dirs)
  35.     with open(dirs+"index.html","w+",encoding="utf-8") as f:
  36.         f.write(moban %关键词)
复制代码
下载安装python3 https://www.python.org/downloads/ 代码存为xx.py 双击运行或IDLE打开F5运行

TOP

本帖最后由 523066680 于 2015-12-10 10:00 编辑

刚刚装了字体monaco
上来一看论坛字体变了

所以,默认字体是monaco?

TOP

回复 6# 523066680


   是不是你设置使用系统默认字体了。
下载安装python3 https://www.python.org/downloads/ 代码存为xx.py 双击运行或IDLE打开F5运行

TOP

回复 7# 依山居


    没有设置过,昨天安装了N个字体测试自己的论坛代码显示效果。
上来Bathome一看也变了。

monaco 字体挺好的,consolas, Dejavu sans mono 也不错

TOP

瞅了下,

等宽, 以下这些容易混淆的东东 易 分辨就好:
  1. l1  Oo08  b6  z2  aqg9   s5
复制代码

TOP

返回列表