下載libdispatch腳本

最近剛接觸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
    #篩選數(shù)據(jù)
    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 "你要保存代碼的地方"
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,326評論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,670評論 4 61
  • 自卑帶給人的痛苦,只有經(jīng)歷過的人才會懂。 不知有多少人和我一樣,對自己總是這樣那樣地不認(rèn)可,已經(jīng)說出口的想要說出口...
    走在青草邊閱讀 423評論 0 0
  • 1999年正式確定了汝窯燒造區(qū)的具體位置和范圍(河南寶豐清涼寺),2000年6月至10月正式發(fā)掘,同年...
    逍遙般若閱讀 573評論 0 2
  • 這是一份專屬于嘻寶寶的新年祝福—— 其實作為一個土生土長的中國人,在根本觀念里,所謂新年是除夕夜之后的日子...
    蘇楚叔叔閱讀 327評論 0 1

友情鏈接更多精彩內(nèi)容