【发布时间】:2020-04-12 11:10:56
【问题描述】:
网页上有一张图片,我想使用 python 将其保存在我的磁盘上。 我试图做的是
r=requests.get(url, timeout=60)
p=os.path.sep.join([args["output"],"{}.jpeg".format(str(total).zfill(5))])
f.write(r.content)
f.close()
但我意识到保存的文件不是图像格式
$file name_of_file
00018.jpeg: HTML document, ASCII text, with very long lines, with no line terminators
然后我尝试:
r=requests.get(url, timeout=60)
p=os.path.sep.join([args["output"],"{}.jpeg".format(str(total).zfill(5))])
f=open(p, "wb")
i=r.raw
q=Image.open(BytesIO(r.content))
print(q.type)
f.write(i)
f.close()
但没有成功。我该怎么办?
更新:
r = requests.get(url, timeout=60)
# save the image to disk
p = os.path.sep.join([args["output"], "{}.jpeg".format(
str(total).zfill(5))])
with open("test.jpeg","wb+") as f:
f.write(requests.get("name_of_website",headers=headers).content)
f.close()
当我使用光标从网上手动复制图像时,它是 jpg 格式。
【问题讨论】:
-
网址是什么?是图片文件吗?
-
只有一张图片
-
Service Interruption....你能正常访问这个页面吗? -
@jizhihaoSAMA 添加了截图。该网站可能仅适用于某些地区
-
你为什么用
f=open(xxx)?你能不能运行我的例子来下载图像来检查图像内容?而且,图像名称是test.jpg而不是00008.jpg。