Python解决Google文档打开缓慢问题

2 篇文章 0 订阅
2 篇文章 0 订阅

分享一个处理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)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值