找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 18336|回复: 8

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

[复制链接]
发表于 2015-11-24 01:55:47 | 显示全部楼层 |阅读模式
  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技术 +1 收起 理由
CrLf + 1 感谢分享

查看全部评分

 楼主| 发表于 2015-12-5 00:24:37 | 显示全部楼层
  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 &#169; 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))
复制代码
 楼主| 发表于 2015-12-5 04:14:33 | 显示全部楼层
  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]))
复制代码
 楼主| 发表于 2015-12-5 04:35:27 | 显示全部楼层
  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 &#169; 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)))
复制代码
 楼主| 发表于 2015-12-10 06:08:35 | 显示全部楼层
几个代码方法略有不同。功能效果是相同的。
  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 &#169; 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 %关键词)
复制代码
发表于 2015-12-10 09:55:30 | 显示全部楼层
本帖最后由 523066680 于 2015-12-10 10:00 编辑

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

所以,默认字体是monaco?
 楼主| 发表于 2015-12-10 10:58:20 | 显示全部楼层
回复 6# 523066680


   是不是你设置使用系统默认字体了。
发表于 2015-12-10 11:37:56 | 显示全部楼层
回复 7# 依山居


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

monaco 字体挺好的,consolas, Dejavu sans mono 也不错
发表于 2015-12-10 13:54:05 | 显示全部楼层
瞅了下,

等宽, 以下这些容易混淆的东东 易 分辨就好:
  1. l1  Oo08  b6  z2  aqg9   s5
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 02:09 , Processed in 0.010074 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表