首页 文章

如何从网址保存GIF? [重复]

提问于
浏览
0

这个问题在这里已有答案:

我正在使用 Giphypop 来检索 gif 的网址 . 为方便起见,我将此url保存在变量中,但现在我需要以某种方式将 gif 保存到文件中 . 但是我收到了一个错误 .

我想这可以重新开放 . 我的问题是Windows保存后没有打开 gif 文件 . 这是问题的代码和截图,抱歉我不能提前发布 .

Code:

import giphypop
from urllib import request

g = giphypop.Giphy()

request.urlretrieve("http://giphy.com/gifs/fullerhouse-3oz8xJfB6XGBwOA8HS", "test.gif")

Screenshot:

enter image description here

3 回答

  • 1

    一旦你有了一个URL,它就像一行一样简单(和导入,公平):

    import urllib
    urllib.urlretrieve('http://example.com/somefile.gif', '/path/to/save/myfile.gif')
    
  • 0

    试试这个:

    import urllib
    foo = urllib.urlretrieve('htttp://www.somelink.com/file.gif','localfilename.gif')
    
  • 0

    如果您不是urllib的粉丝,可以尝试使用请求模块:

    import requests
    
    url = "http://www.url.of/the-file/here.gif"
    response = requests.get(url)
    data = response.text
    open("./image.gif", "w").write(data)
    

    或者您也可以尝试使用上面代码的较短替代方法:

    import requests
    
    download = lambda url, filename="": open(filename if filename else (url.split("/")[-1] or "file.html"), "w").write(requests.get(url).text)
    download("http://www.url.of/the-file/here.gif", "image.gif")
    

相关问题