通过Python的urllib保存GIF文件

2024-04-26 05:53:59 发布

您现在位置:Python中文网/ 问答频道 /正文

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

(venv) C:\Users\NBenton\PycharmProjects\RES3D_BETA>python Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 16:07:46) [MSC v.1900 32 bit (Intel)] on win32

我试过使用以下两个脚本通过URL下载一个GIF文件。你知道吗

(一)

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")

(二)

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)在查看时都是这样显示的:

Output

有人能就这个问题提出一些见解吗?stackoverflow上确实存在类似(几乎相同)的情况,但与此不同。你知道吗


Tags: 文件fromimport脚本httprequestwwwurllib
1条回答
网友
1楼 · 发布于 2024-04-26 05:53:59

正在检索的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

相关问题 更多 >

    热门问题