美文网首页
下载libdispatch脚本

下载libdispatch脚本

作者: gpr | 来源:发表于2017-09-10 17:48 被阅读135次

最近刚接触python,解决了一个以前无法解决的问题,就是下载苹果开源项目的代码问题,话不多说送上我写的脚本,希望对你有用:

#!/usr/bin/python
# coding=utf-8
import requests
import os
import re
import sys, getopt

def download_file(url, home_path):
    if not home_path.endswith("/"):
        home_path = home_path + "/"

    if not url.endswith("/"):
        url = url + "/"

    print "url: "+ url
    print "home_path: " + home_path
    r = requests.get(url)
    html = r.text
    #筛选数据
    text_array = html.split("\n")
    for line in text_array:
        match = re.match(r'^(<tr>){1}(<td.*</td>){3}(</tr>){1}$', line)
        match1 = re.findall(r'<td valign="top"><a href=".*"><img', line)
        if match and match1:
            #先搞定可以直接下载的
            match2 = re.findall(r'"\w+\.?\w*"', match1[0])
            if match2.__len__() == 2:
                file_name = match2[1].replace('"', "")
                file_path = home_path + file_name
                file_download_url = url + file_name
                if os.path.exists(file_path):
                    os.remove(file_path)
                file_r = requests.get(file_download_url)
                with open(file_path, "wb") as code:
                    code.write(file_r.content)
                print("write file " + file_name + " at:" + file_path)

            #在搞定有文件目录的
            match3 = re.findall(r'"\w+\.?\w*/"', match1[0])
            if match3:
                dir_name = match3[0].replace('"', "")
                home_path_tmp = home_path + dir_name
                if os.path.exists(home_path_tmp):
                    os.popen('rm -rf ' + home_path_tmp)
                os.mkdir(home_path_tmp)
                url_tmp = url + dir_name
                download_file(url_tmp, home_path_tmp)

def main(argv):
    url = ""
    home_path = ""
    try:
        options, args = getopt.getopt(argv, "hu:o:", ["help", "url=", "output="])
        for option, value in options:
            if option in ("-h", "--help"):
                print("""
                -u --url:    输入的url
                -o --output: 保存的路径
                      """)
            if option in ("-u", "--url"):
                url = "" + value
            elif option in ("-o", "--output"):
                home_path = "" + value
            else:
                print "unknow arg: " + value
                sys.exit()
    except getopt.GetoptError:
        sys.exit()

    download_file(url, home_path)

if __name__ == '__main__':
    main(sys.argv[1:])

使用方法:

python Test.py -u "https://opensource.apple.com/source/libdispatch/libdispatch-187.10/" -o "你要保存代码的地方"

相关文章

网友评论

      本文标题:下载libdispatch脚本

      本文链接:https://www.haomeiwen.com/subject/azltsxtx.html