[英]Image downloading in python error handling
我有包含圖像的文本文件,並且有一個我想保存圖像的目錄。
import os
import urllib
import sys
def normalize(url):
url = url.split("/")[-1]
return url.split("\n")[0]
def main():
out_dir = "Workspace/cnf/img"
with open('cnf/image_flower.txt') as url_array:
for url in url_array:
try:
urllib.urlretrieve(url, os.path.join(out_dir, normalize(url)))
except Exception as e :
print "Exception|",e,"|",url
print("Images Downloaded")
主要()
我正在獲取圖像。但是我面臨以下問題1.我想捕獲所有http錯誤代碼並像未找到的頁面和所有錯誤那樣打印它,但我的代碼無法打印。 2.我有大約1,00,000個網址,所以我的代碼要花很多時間。
您能否建議我一種更好的處理方法
try: urllib.urlretrieve(url, os.path.join(out_dir, normalize(url))) except Exception as e : print "Exception: ",e," at ",url
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.