且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过Python处理Android API Doc离线访问

更新时间:2021-09-11 17:27:35

原因大家应该都知道,离线下载的SDK Api本地也无法打开,其实主要就是因为这些Doc中有去访问google的一些网站:font、js api等等,因此,要真正离线使用Doc,有两个方法可以实现:


1、真正的离线——即把网断掉,这样确实可以,但是,使用起来太不方便了


2、把API Doc中的所有请求font、js api的内容都删掉,不过,这个过程太痛苦了,API Doc有几万个文件,总不能一个个删,所以,祭出Python,秒秒钟搞定,代码如下:

import os
s1 = '''<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:light,regular,medium,thin,italic,mediumitalic,bold" title="roboto">'''
s2 = '''<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto+Condensed">'''
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'/data/SDK/sdk/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()


使用时只要将os.walk的路径修改成doc的路径即可,运行后很快就能完成全部的替换,如果碰到某些页面还是打不开,只需要打开源代码,找到访问google的请求加入到脚本中进行替换即可。


以上。