【问题标题】:File Not Found Error while Downloading Image files下载图像文件时未找到文件错误
【发布时间】:2021-06-02 20:31:36
【问题描述】:

我使用的是 Windows 8.1,所以我最近一直在进行网络抓取,并且也非常成功地发现了一些错误,但现在我一直在下载文件,因为它们不会下载并给我一个

FileNotFoundError.

我已从名称文件中删除了所有未知字符,但仍然出现此错误。任何帮助。

为了以防万一,我还把名字改成了小写。当我下载第 22 个项目时发生错误,其他项目在第 22 个之前下载正常。

我的代码和 Excel 文件供参考:

import time
import pandas as pd
import requests

Final1 = pd.read_excel("Sneakers.xlsx")
Final1.index+=1

a = Final1.index.tolist()
Images = Final1["Images"].tolist()
Name = Final1["Name"].str.lower().tolist()
Brand = Final1["Brand"].str.lower().tolist()

s = requests.Session()

for i,n,b,l in zip(a,Name,Brand,Images):
    r = s.get(l).content
    with open("Images//" + f"{i}-{n}-{b}.jpg","wb") as f:
        f.write(r)

Excel 文件(Google 云端硬盘):Excel File

【问题讨论】:

  • 您使用的是 Windows 吗?如果是,那么您可能需要一个反斜杠而不是正斜杠更改 with open("Images//" + f"{i}-{n}-{b}.jpg","wb") as f:with open("Images\\" + f"{i}-{n}-{b}.jpg","wb") as f:
  • 嗨,我试过了,但仍然出现同样的错误 FileNotFoundError: [Errno 2] No such file or directory: 'Images\\22-textured 斑点端口鞋面新设计收藏版时尚奢华休闲跑步派对服篮球鞋男童运动鞋 高品质运动鞋 男士运动鞋-crownprince.jpg
  • 当我下载第 22 个项目时发生错误,其他项目在第 22 个之前下载正常

标签: python python-3.x pandas error-handling python-requests


【解决方案1】:
  1. 您的路径中似乎没有 Images 文件夹。
  2. 在python中使用os.path.join()函数加入路径会更好。

试试下面:

import os
import time
import pandas as pd
import requests

Final1 = pd.read_excel("Sneakers.xlsx")
Final1.index+=1

a = Final1.index.tolist()
Images = Final1["Images"].tolist()
Name = Final1["Name"].str.lower().tolist()
Brand = Final1["Brand"].str.lower().tolist()

# Added
if not os.path.exists("Images"):
    os.mkdir("Images")

s = requests.Session()
for i,n,b,l in zip(a,Name,Brand,Images):
    r = s.get(l).content
    # with open("Images//" + f"{i}-{n}-{b}.jpg","wb") as f:
    with open(os.path.join("Images", f"{i}-{n}-{b}.jpg"),"wb") as f:
        f.write(r)

【讨论】:

  • 嗨,我也试过了。。我仍然得到同样的错误:(当它到达第 22 个图像时发生错误,之前的 1-21 下载正常
  • 你能告诉我错误信息是什么吗?因为当我尝试时,它适用于最后一个文件(160-combo(r)-628-995 sneakers for men-swiggy.jpg)
  • 嗨,我刚刚删除了导致问题的特定条目。这就是我猜的全部
猜你喜欢
  • 1970-01-01
  • 2016-08-16
  • 2022-09-27
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多