最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python根據(jù)URL地址下載文件并保存至對應(yīng)目錄的實現(xiàn)

 更新時間:2020年11月15日 15:25:25   作者:媽噠好氣哦  
這篇文章主要介紹了Python根據(jù)URL地址下載文件并保存至對應(yīng)目錄的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

引言

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

Python 3.6.5,Anaconda, VSCode

1. 下載數(shù)據(jù)集文件

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

在這里插入圖片描述

 2. 獲取樣本文件位置

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

import os


def get_file(root_path, all_files={}):
  '''
  遞歸函數(shù),遍歷該文檔目錄和子目錄下的所有文件,獲取其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_files


if __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 創(chuàng)建文件夾

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 os


def get_file(root_path, all_files={}):
  '''
  遞歸函數(shù),遍歷該文檔目錄和子目錄下的所有文件,獲取其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_files


if __name__ == '__main__':
  path = './raw_data'
  print(get_file(path))

4.2 get_pic.py

import get_doc_path
import os
import urllib.request


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)


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 運行結(jié)果

執(zhí)行get_pic.py
當(dāng)程序意外停止或再次執(zhí)行時,程序會自動跳過文件夾中已下載的文件,繼續(xù)下載未下載的內(nèi)容

{‘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.txt
Try downloading file: http://41.media.tumblr.com/xxxxxx.jpg
Try downloading file: http://41.media.tumblr.com/xxxxxx.jpg
Try downloading file: http://ak1.polyvoreimg.com/cgi/img-thing/size/l/tid/xxxxxx.jpg
Error occurred when downloading file, error message:
HTTP Error 502: No data received from server or forwarder
Try downloading file: http://akicocotte.weblike.jp/gaugau/xxxxxx.jpg
Try downloading file: http://animewriter.files.wordpress.com/2009/01/nagisa-xxxxxx-xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg
Try downloading file: http://cdn.awwni.me/xxxxxx.jpg

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

20.9.23更新:數(shù)據(jù)集地址:https://github.com/ZQ-Qi/nsfw_data_scrapper,單純?yōu)榱藢W(xué)習(xí)和實踐本文代碼的可以下載該數(shù)據(jù)集進行嘗試

到此這篇關(guān)于Python根據(jù)URL地址下載文件并保存至對應(yīng)目錄的實現(xiàn)的文章就介紹到這了,更多相關(guān)Python URL下載文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何解決Python:報錯[Errno 2]No such file or directory問題

    如何解決Python:報錯[Errno 2]No such file or&nb

    介紹了Python文件讀取操作時常見的錯誤原因及解決方法,主要錯誤原因包括路徑拼寫錯誤、工作目錄與相對路徑不匹配以及文件不存在,解決方法有使用絕對路徑和動態(tài)獲取腳本路徑,其他注意事項包括驗證文件路徑與名稱、理解工作目錄與相對路徑
    2025-02-02
  • 淺談pytorch、cuda、python的版本對齊問題

    淺談pytorch、cuda、python的版本對齊問題

    今天小編就為大家分享一篇淺談pytorch、cuda、python的版本對齊問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python多進程下實現(xiàn)日志記錄按時間分割

    python多進程下實現(xiàn)日志記錄按時間分割

    這篇文章主要為大家詳細介紹了python多進程下實現(xiàn)日志記錄按時間分割,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • python3.8下載及安裝步驟詳解

    python3.8下載及安裝步驟詳解

    這篇文章主要介紹了python3.8下載及安裝步驟詳解,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01
  • django框架使用orm實現(xiàn)批量更新數(shù)據(jù)的方法

    django框架使用orm實現(xiàn)批量更新數(shù)據(jù)的方法

    這篇文章主要介紹了django框架使用orm實現(xiàn)批量更新數(shù)據(jù)的方法,結(jié)合實例形式簡單分析了Django基于orm操作數(shù)據(jù)庫更新數(shù)據(jù)的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2019-06-06
  • Django Admin 管理工具的實現(xiàn)

    Django Admin 管理工具的實現(xiàn)

    這篇文章主要介紹了Django Admin 管理工具的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • python函數(shù)存儲在模塊的優(yōu)點及用法總結(jié)

    python函數(shù)存儲在模塊的優(yōu)點及用法總結(jié)

    在本篇文章里小編給大家整理了一篇關(guān)于python函數(shù)存儲在模塊的優(yōu)點及用法相關(guān)內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。
    2021-10-10
  • Python自動打印被調(diào)用函數(shù)變量名及對應(yīng)值?

    Python自動打印被調(diào)用函數(shù)變量名及對應(yīng)值?

    這篇文章主要介紹了Python自動打印被調(diào)用函數(shù)的變量名及對應(yīng)的值,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價,需要的朋友可以參考一下?
    2022-07-07
  • 利用python實時刷新基金估值效果(摸魚小工具)

    利用python實時刷新基金估值效果(摸魚小工具)

    這篇文章主要介紹了利用python實時刷新基金估值(摸魚小工具),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • python科學(xué)計算之numpy——ufunc函數(shù)用法

    python科學(xué)計算之numpy——ufunc函數(shù)用法

    今天小編就為大家分享一篇python科學(xué)計算之numpy——ufunc函數(shù)用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11

最新評論

怀宁县| 四子王旗| 乌鲁木齐县| 蛟河市| 湛江市| 顺平县| 外汇| 宿迁市| 锡林郭勒盟| 海门市| 巴林右旗| 视频| 色达县| 县级市| 安福县| 梅河口市| 安达市| 民勤县| 镇坪县| 铜陵市| 威宁| 醴陵市| 乐都县| 织金县| 泰安市| 张家港市| 彭州市| 五原县| 湖南省| 临漳县| 济源市| 北碚区| 革吉县| 荔浦县| 独山县| 宿迁市| 宁海县| 泰宁县| 温州市| 宁安市| 讷河市|