分享一个处理Google API 开发文档的小工具,可避开讨厌的龟速加载过程或必须断网查看的麻烦,最近学习Python,所以就用它写了个文本处理脚本.
大致思路:
1.找出docs中加载耗时的资源请求链接,Chrome中F12打开,查看Network项(按时间筛选),当然不是所有耗时链接都得删除的(web小白也不懂),复制请求包含的链接,然后在网页源码中搜索.复制需要删除的代码块.添加到Python脚本中.
2.使用Python遍历替换掉所有包含上述代码块的文档内容.
(把文档的外部链入的css样式去掉了,断网能用,要这个干嘛?于是乎咔!其它能删的有待网友补充了.)
#coding=utf-8
import os
#输入文件路劲
fpath = raw_input('Input Android.docs path:')
#需要删除的代码块添加到maps内,做为key,value值为空""
maps={
'<li><a href="https://play.google.com/apps/publish/" target="_googleplay">Google Play Developer Console</a></li>':"",
'<li><a href="http://android-developers.blogspot.com/">Android Developers Blog</a></li>':"",
'''<link rel="stylesheet"
href="http://fonts.googleapis.com/css?family=Roboto+Condensed">''':"",
'<script src="http://www.google.com/jsapi" type="text/javascript"></script>':"",
'''<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:light,regular,medium,thin,italic,mediumitalic,bold"
title="roboto">''':"",
'''<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto+Condensed">''':"",
'''<script type="text/javascript" async="" src="https://apis.google.com/js/plusone.js"></script>''':"",
'''<script type="text/javascript" async="" src="http://www.google-analytics.com/ga.js"></script>''':"",
'''<li><a href="http://www.android.com">Android.com</a></li>''':"",
'''<li><a href="http://source.android.com">Android Open Source Project</a></li>''':"",
'''<script src="https://developer.android.com/ytblogger_lists_unified.js" type="text/javascript"></script>''':""
}
#记录处理的文档个数
#i = 1
for root,dirs,files in os.walk(fpath):
for file in files:
fd = root + os.sep + file
if ".html" in fd:
#print fd ,'-->',i
#i+=1
with open(fd, 'r+') as f:
text = f.read()
for j in maps:
text = text.replace(j,keys[j])
with open(fd, 'w') as f:
f.write(text)