【问题标题】:How to make python ignore a blank cell and continue downloading images from the next cell如何让python忽略一个空白单元格并继续从下一个单元格下载图像
【发布时间】:2022-01-22 17:58:09
【问题描述】:

当遍历单元格时 如果出现空白单元格,并弹出错误并停止下载。是否有任何例外或步骤可以忽略空白单元格?

for j in u.iteritems():
       
        file_name = str(i)+".jpeg"
        res = requests.get(u[0], stream = True)

        if res.status_code == 200:
            with open(file_name,'wb') as f:
                shutil.copyfileobj(res.raw, f) 
            print('Image sucessfully Downloaded: ',file_name)
        else:
            print('Image Couldn\'t be retrieved')

【问题讨论】:

    标签: pandas python-requests shutil


    【解决方案1】:

    尝试添加一个 try: ..., except: continue 块(或 try:..., except: pass 块) 像这样:

    for j in u.iteritems():
        try:
           file_name = str(j)+".jpeg"
           res = requests.get(u[0], stream = True)
    
           if res.status_code == 200:
               with open(file_name,'wb') as f:
                  shutil.copyfileobj(res.raw, f) 
               print('Image sucessfully Downloaded: ',file_name)
           else:
               print('Image Couldn\'t be retrieved')
        except:
           continue
    

    【讨论】:

    • 我不知道如何在这里添加异常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 2019-02-27
    • 1970-01-01
    • 2023-02-23
    • 2016-03-24
    • 1970-01-01
    • 2018-11-30
    相关资源
    最近更新 更多