peclian搭建网站以及一些细节的处理

参考链接
微信搜索我吃你家米了关注公众号
在这里插入图片描述

想在网站上添加站长之家的统计脚本

但是不知道怎可麽添加

于是用了一个笨方法,直接遍历所有html文件,将footer替换掉

由于是多行替换,需要先使用strip将多行文本转换为单行文本,然后使用replace进行替换

具体代码如下:

render.py:

import os
import fileinput

def get_filelist(dir):
    newDir = dir
    if os.path.isfile(dir):
        if os.path.splitext(dir)[-1] == ".html":
            replace(dir)
    elif os.path.isdir(dir):
        for s in os.listdir(dir):
            newDir=os.path.join(dir,s)
            get_filelist(newDir)
        
def replace(dir):
    after = """<footer id="contentinfo" class="body">                    <address id="about" class="vcard body">                    Proudly powered by <a href="http://getpelican.com/">Pelican</a>, which takes great advantage of <a href="http://python.org">Python</a>.                    </address><!-- /#about --><p>The theme is by <a href="http://coding.smashingmagazine.com/2009/08/04/designing-a-html-5-layout-from-scratch/">Smashing Magazine</a>, thanks!<br/><script type="text/javascript">document.write(unescape("%3Cspan id='cnzz_stat_icon_1278862900'%3E%3C/span%3E%3Cscript src='https://s9.cnzz.com/z_stat.php%3Fid%3D1278862900%26show%3Dpic' type='text/javascript'%3E%3C/script%3E"));</script></p>            </footer><!-- /#contentinfo -->"""

    before = """<footer id="contentinfo" class="body">	<address id="about" class="vcard body">	Proudly powered by <a href="http://getpelican.com/">Pelican</a>, which takes great advantage of <a href="http://python.org">Python</a>.	</address><!-- /#about -->		<p>The theme is by <a href="http://coding.smashingmagazine.com/2009/08/04/designing-a-html-5-layout-from-scratch/">Smashing Magazine</a>, thanks!</p>	</footer><!-- /#contentinfo -->"""
    
    #read input file
    print(dir)
    fin = open(dir, "rt")
    #read file contents to string
    lines = fin.readlines()
    data = '\t'.join([line.strip() for line in lines])

    #replace all occurrences of the required string
    data = data.replace(before, after)
    #close the input file
    fin.close()
    #open the input file in write mode
    fin = open(dir, "wt")
    #overrite the input file with the resulting data

    fin.write(data)
    #close the file
    fin.close()
            
if __name__ =="__main__":
    get_filelist("/var/www/html")

大致思想就是将文件打开,然后将data变量处理成单行的,然后再使用replace进行替换,最后将修改完成后的内容写回到文件中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值