【问题标题】:Saving an image from webpage using request使用请求从网页保存图像
【发布时间】: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

标签: python image request


【解决方案1】:

此页面需要 cookie 来执行此操作:

如果没有,则不能直接访问。

一种简单的方法是在您的请求标头中添加一个 cookie:

import requests

headers = {
    "Cookie":"visid_incap_276192=vO9ugmNqRS+XGehZnF1jiwL8kl4AAAAAQUIPAAAAAADc6Z+46+Lp6X9DL0FUaSOv; incap_ses_627_276192=HgPZUq1t1yD2FURXnY2zCAL8kl4AAAAAyQ+1ZeYdSVzPTcurvHnlwA==; JSESSIONID=0001Zh35TV6HDxcVflnHMwIHsqe:-1801K8D; incap_ses_553_276192=XuxOZn9AsVOTcVuFwKasB3P9kl4AAAAAaxsIzIzT5BwV8RqhcTVPsw==",
}

with open("test.jpg","wb+") as f:
    f.write(requests.get("https://www.e-zpassny.com/vector/jcaptcha.do",headers=headers).content)

现在它可以成功下载图像了:

【讨论】:

  • 文件在我打开时下载,它们是空的。 file 00008.jpg' gives 00008.jpg: 空`
  • 我已经更新了关于如何运行脚本的问题,我还添加了 cookie
  • 为什么不用print(response.context)来检查response的字节数?如果是b'xxxxx',就代表你访问图片成功了。
【解决方案2】:

我认为你应该这样做:

r = requests.get(url, timeout=60)
q = Image.open(BytesIO(r.content))
fp = os.path.join([args["output"], f"{str(total).zfill(5)}.jpeg"]) # here i used f-string because it looks more compact 
q = q.save(fp)

Image.save() 描述here
F-strings 它的格式化方式,它描述了herehere

希望对你有帮助,祝你有个美好的一天!

编辑: 好的,它看起来不起作用 所以,你可以从here试试这个:

r = requests.get(url, timeout=60)

bytes = BytesIO(r.content)
bytes.seek(0)
q = Image.ope(bytes)

fp = os.path.join([args["output"], f"{str(total).zfill(5)}.jpeg"]) # here i used f-string because it looks more compact 
q = q.save(fp)

【讨论】:

  • 我在 q=Image.open(Byt...) 行收到此错误 "cannot identify image file %r" % (filename if filename else fp) PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7febf9fcfa98>
  • 由于一个奇怪的原因,我得到了同样的错误。有没有其他方法可以在不使用请求的情况下提取图像
  • @Hrushi 我不知道,但我相信您可以使用Image.open(BytesIO(response.content)).show() 显示图像,因为我前段时间已经这样做了。实际上,我不知道如何在没有请求的情况下执行此操作,但我认为如果您可以显示它,您也可以将其保存在文件夹中。或许this可以帮到你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-08
  • 2014-04-06
相关资源
最近更新 更多