【问题标题】:Saving GIF File via Python's urllib通过 Python urllib 保存 GIF 文件
【发布时间】:2018-11-01 20:39:43
【问题描述】:

请注意,我使用的是以下版本的 Python:

(venv) C:\Users\NBenton\PycharmProjects\RES3D_BETA>python Python 3.6.5 (v3.6.5:f59c0932b4,2018 年 3 月 28 日,16:07:46)[MSC v.1900 32 位(英特尔)] 在win32上

我尝试使用以下两个脚本通过 URL 下载单个 GIF 文件。

1)

from urllib.request import urlretrieve

urlretrieve('http://www.iframeapps.dcnr.state.pa.us/topogeo/PaGWIS_search/DisplayReportImage.aspx?id=IM209132',
            "C:/Users/NBenton/PycharmProjects/RES3D_BETA/image1.gif")

2)

from urllib import request

request.urlretrieve("http://www.iframeapps.dcnr.state.pa.us/topogeo/PaGWIS_search/DisplayReportImage.aspx?id=IM209132",
                    "image2.gif")

对于这些脚本中的每一个,进程都以退出代码 0 结束 - 因此,据我所知,没有发生任何灾难性事件。

但是,两个输出文件(image1.gif 和 image2.gif)在查看每个文件时都是这样显示的:

谁能提供一些关于这个问题的见解?确实类似(几乎相同)的案例在 stackoverflow 上,但不是这样。

【问题讨论】:

  • 这可能与gif 格式本身有关。尝试将文件格式 (gif) 与照片查看器相关联。
  • 你下载的是html,一定要获取图片的url:http://www.iframeapps.dcnr.state.pa.us/topogeo/PaGWIS_search/WellReports/200001-210000/IM209132.gif
  • @eyllanesc 成功了!您如何/在哪里检索到信息“...200001-210000...”?另外,为什么会这样?

标签: python urllib


【解决方案1】:

检索到的 URL 不是图像源,而是包含图像的 HTML 文档。

运行时(或在浏览器中打开 URL):

curl http://www.iframeapps.dcnr.state.pa.us/topogeo/PaGWIS_search/DisplayReportImage.aspx?id=IM209132

您可以在文档的源文档中查看图片src

<!--...-->
<div style="text-align: center;">
    <img id="imgPhoto" src="./WellReports/<maskedpath>/IM209132.gif" width="100%" border="0" />
</div>

图片的src 表明它的位置是相对于 URL 路径的,即

http://www.iframeapps.dcnr.state.pa.us/topogeo/PaGWIS_search/WellReports/<maskedpath>/IM209132.gif

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 2018-11-12
    • 2019-06-27
    • 2020-09-15
    • 2019-01-19
    相关资源
    最近更新 更多