亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Python根據URL地址下載文件并保存至對應目錄的實現

瀏覽:4日期:2022-07-05 13:50:15

引言

在編程中經常會遇到圖片等數據集將圖片等數據以URL形式存儲在txt文檔中,為便于后續的分析,需要將其下載下來,并按照文件夾分類存儲。本文以Github中Alexander Kim提供的圖片分類數據集為例,下載其提供的圖片樣本并分類保存

Python 3.6.5,Anaconda, VSCode

1. 下載數據集文件

建立項目文件夾,下載上述Github項目中的raw_data文件夾,并保存至項目目錄中。

Python根據URL地址下載文件并保存至對應目錄的實現

2. 獲取樣本文件位置

編寫get_doc_path.py,根據根目錄位置,獲取目錄及其子目錄所有數據集文件

import osdef get_file(root_path, all_files={}): ’’’ 遞歸函數,遍歷該文檔目錄和子目錄下的所有文件,獲取其path ’’’ files = os.listdir(root_path) for file in files: if not os.path.isdir(root_path + ’/’ + file): # not a dir all_files[file] = root_path + ’/’ + file else: # is a dir get_file((root_path+’/’+file), all_files) return all_filesif __name__ == ’__main__’: path = ’./raw_data’ print(get_file(path))

3. 下載文件

3.1 讀取url列表并

for filename, path in paths.items(): print(’reading file: {}’.format(filename)) with open(path, ’r’) as f: lines = f.readlines() url_list = [] for line in lines:url_list.append(line.strip(’n’)) print(url_list)

3.2 創建文件夾

foldername = './picture_get_by_url/pic_download/{}'.format(filename.split(’.’)[0])if not os.path.exists(folder_path): print('Selected folder not exist, try to create it.') os.makedirs(folder_path)

3.3 下載圖片

def get_pic_by_url(folder_path, lists): if not os.path.exists(folder_path): print('Selected folder not exist, try to create it.') os.makedirs(folder_path) for url in lists: print('Try downloading file: {}'.format(url)) filename = url.split(’/’)[-1] filepath = folder_path + ’/’ + filename if os.path.exists(filepath): print('File have already exist. skip') else: try:urllib.request.urlretrieve(url, filename=filepath) except Exception as e:print('Error occurred when downloading file, error message:')print(e)

4. 完整源碼

4.1 get_doc_path.py

import osdef get_file(root_path, all_files={}): ’’’ 遞歸函數,遍歷該文檔目錄和子目錄下的所有文件,獲取其path ’’’ files = os.listdir(root_path) for file in files: if not os.path.isdir(root_path + ’/’ + file): # not a dir all_files[file] = root_path + ’/’ + file else: # is a dir get_file((root_path+’/’+file), all_files) return all_filesif __name__ == ’__main__’: path = ’./raw_data’ print(get_file(path))

4.2 get_pic.py

import get_doc_pathimport osimport urllib.requestdef get_pic_by_url(folder_path, lists): if not os.path.exists(folder_path): print('Selected folder not exist, try to create it.') os.makedirs(folder_path) for url in lists: print('Try downloading file: {}'.format(url)) filename = url.split(’/’)[-1] filepath = folder_path + ’/’ + filename if os.path.exists(filepath): print('File have already exist. skip') else: try:urllib.request.urlretrieve(url, filename=filepath) except Exception as e:print('Error occurred when downloading file, error message:')print(e)if __name__ == '__main__': root_path = ’./picture_get_by_url/raw_data’ paths = get_doc_path.get_file(root_path) print(paths) for filename, path in paths.items(): print(’reading file: {}’.format(filename)) with open(path, ’r’) as f: lines = f.readlines() url_list = [] for line in lines:url_list.append(line.strip(’n’)) foldername = './picture_get_by_url/pic_download/{}'.format(filename.split(’.’)[0]) get_pic_by_url(foldername, url_list)

4.3 運行結果

執行get_pic.py當程序意外停止或再次執行時,程序會自動跳過文件夾中已下載的文件,繼續下載未下載的內容

{‘urls_drawings.txt’: ‘./picture_get_by_url/raw_data/drawings/urls_drawings.txt’, ‘urls_hentai.txt’: ‘./picture_get_by_url/raw_data/hentai/urls_hentai.txt’, ‘urls_neutral.txt’: ‘./picture_get_by_url/raw_data/neutral/urls_neutral.txt’, ‘urls_porn.txt’: ‘./picture_get_by_url/raw_data/porn/urls_porn.txt’, ‘urls_sexy.txt’: ‘./picture_get_by_url/raw_data/sexy/urls_sexy.txt’}reading file: urls_drawings.txtTry downloading file: http://41.media.tumblr.com/xxxxxx.jpgTry downloading file: http://41.media.tumblr.com/xxxxxx.jpgTry downloading file: http://ak1.polyvoreimg.com/cgi/img-thing/size/l/tid/xxxxxx.jpgError occurred when downloading file, error message:HTTP Error 502: No data received from server or forwarderTry downloading file: http://akicocotte.weblike.jp/gaugau/xxxxxx.jpgTry downloading file: http://animewriter.files.wordpress.com/2009/01/nagisa-xxxxxx-xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpgTry downloading file: http://cdn.awwni.me/xxxxxx.jpg

后注:由于樣本數據集內容的問題,上述地址以xxxxx代替具體地址,案例項目也已經失效,但是方法仍然可以借鑒

20.9.23更新:數據集地址:https://github.com/ZQ-Qi/nsfw_data_scrapper,單純為了學習和實踐本文代碼的可以下載該數據集進行嘗試

到此這篇關于Python根據URL地址下載文件并保存至對應目錄的實現的文章就介紹到這了,更多相關Python URL下載文件內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 欧美成人高清手机在线视频 | 麻豆精品视频网站在线观看 | 亚洲 欧美 综合 | 欧美综合一区 | 久久综合中文字幕一区二区 | 亚洲国产精品aaa一区 | 免费视频久久久 | 久久黄色免费网站 | 精品欧美一区二区在线观看 | 亚洲国产成人久久一区www妖精 | 麻豆福利视频 | 亚洲影视一区 | 国产伦精品一区二区 | 伊人久久大香线蕉精品哪里 | 国产成人精品免费久久久久 | 日本欧美中文字幕人在线 | 97se狠狠狠狠狠亚洲综合网 | 久久久久亚洲香蕉网 | 国产成人高清精品免费软件 | 制服丝袜在线不卡 | 欧美一级毛片欧美一级无片 | 国产在亚洲线视频观看 | 日韩精品一二三区 | 78m成人亚洲| 亚洲精品视频久久久 | 日韩毛片在线影视 | 尤物国产精品福利三区 | 亚洲人与牲动交xxxxbbbb | 99久久精品免费观看区一 | 免费大片在线观看高清 | 成人福利在线播放 | 亚洲高清国产一线久久 | 高h喷水荡肉爽文1v1 | 免费观看黄色小视频 | 超h福利视频在线观看 | 欧美a在线视频 | 香蕉网站狼人久久五月亭亭 | 特级黄色片视频 | 欧美视频在线观看网站 | 日韩欧美视频 | 91成人免费观看网站 |