因为Doc目录下的html文件里含有访问google的js文件
- <link rel="stylesheet"
- href="http://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic,bold" title="roboto">
- <script src="http://www.google.com/jsapi" type="text/javascript"></script>
由于这两行脚本需在线访问Google,
python代码批量删除
思路:遍历doc或docs目录及子目录,查找所有.html文件,打开这些文件,读取文件内容,替换上边的js内容为空,把修改内容写回文件,结束。
说着很复杂,用python实现真的很简单。
- import os
- s1 = '''<link rel="stylesheet"
- href="http://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic,bold" title="roboto">'''
- s2 = '''<script src="http://www.google.com/jsapi" type="text/javascript"></script>'''
- s3 = '''<script type="text/javascript" async="" src="https://apis.google.com/js/plusone.js"></script>'''
- s4 = '''<script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script>'''
- for root,dirs,files in os.walk(r'C:\AndroidSdk\docs'):
- for file in files:
- fd = root + os.sep + file
- if ".html" in fd:
- print fd
- f = open(fd, 'r')
- s = f.read().replace(s1, "").replace(s2, "").replace(s3, "").replace(s4, "")
- f.close()
- f = open(fd, 'w')
- f.write(s)
- f.close()
献丑一条条解释一下,假定我的Android的开发帮助文档在c:\androidsdk\docs下,遍历其下所有目录和文件名只需用os的walk函数即可完成。
- for root,dirs,files in os.walk(r'C:\AndroidSdk\docs'):
去掉html文件里两条影响速度的js,得先有html文件,
- for file in files:
- fd = root + os.sep + file
- if ".html" in fd:
- print fd
- f = open(fd, 'r')
- s = f.read().replace(s1, "").replace(s2, "").replace(s3, "").replace(s4, "")
- f.close()
- f = open(fd, 'w')
- f.write(s)