我想从一个公共的google drive文件夹下载一个数据集(保存为zip)。
第一个月
因为我希望它可以被其他人复制,所以我不想将它复制到我的驱动器上(最好也不要将我的驱动器挂载到笔记本电脑中)。
如何才能做到这一点?
到目前为止,我试过:
import requests
import io
import zipfile
zip_url = 'https://drive.google.com/file/d/1fdFu5NGXe4rTLYKD5wOqk9dl-eJOefXo'
response = requests.get(zip_url)
file_contents = io.BytesIO(response.content)
print(file_contents)
with zipfile.ZipFile(file_contents, 'r') as zip_ref:
zip_ref.extractall('/content/') # Replace with your desired extraction path
字符串
但是得到这个错误(并在之前打印“file_contents”):
<_io.BytesIO object at 0x7ad7efbf27f0>
---------------------------------------------------------------------------
BadZipFile Traceback (most recent call last)
<ipython-input-18-56d2c8f2bfe8> in <cell line: 14>()
12 print(file_contents)
13 # Extract the zip file (if needed)
---> 14 with zipfile.ZipFile(file_contents, 'r') as zip_ref:
15 zip_ref.extractall('/content/') # Replace with your desired extraction path
1 frames
/usr/lib/python3.10/zipfile.py in _RealGetContents(self)
1334 raise BadZipFile("File is not a zip file")
1335 if not endrec:
-> 1336 raise BadZipFile("File is not a zip file")
1337 if self.debug > 1:
1338 print(endrec)
BadZipFile: File is not a zip file
型
如果我尝试以下方法,我会得到一个空的zip文件:
file_id = '1fdFu5NGXe4rTLYKD5wOqk9dl-eJOefXo'
download_url = f'https://drive.google.com/uc?export=download&id={file_id}'
!wget --no-check-certificate -O '/content/file.zip' 'https://drive.google.com/uc?export=download&id=1fdFu5NGXe4rTLYKD5wOqk9dl-eJOefXo'
型
任何帮助将不胜感激。
1条答案
按热度按时间vjrehmav1#
返回的内容类型是html而不是zip文件。
字符串
这应该告诉你服务器返回的内容。在这种情况下,它的文本/html,这不是一个zip文件。
检查URL是否指向实际的zip文件。