0

I'm using Giphypop to retrieve the url of a gif. I saved this url in a variable for convenience, but now I need to somehow save the gif to file. But I'm getting an error.

I think this can be reopened. My problem is Windows not opening the gif file after saving. Here is the code and a screenshot of the problem, sorry I couldn't post earlier.

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

whatwhatwhat
  • 1,991
  • 4
  • 31
  • 50
  • Did Windows 10 tell you why it could not open it? Did you verify if the downloaded file indeed is a GIF? Can Windows 10 open the file if you download it any other way? (Also, where is your code? ) – Jongware Jan 03 '17 at 14:49
  • @RadLexus now, Windows just said something like ''we couldn't open the file''. No helpful details. I'm pretty sure Windows would be able to open it properly if I download it the normal way, I'm just trying to learn how to do all of this via a script. – whatwhatwhat Jan 03 '17 at 14:58
  • 1
    Please provide more details. This is too broad – frlan Jan 03 '17 at 14:58
  • @frlan which part is unclear? I have the url of a gif and I need to download and save it to my hard drive using a script. – whatwhatwhat Jan 03 '17 at 15:00
  • 1
    you should provide the script you use to save the file, otherwise we won't be able to help you – ᴄʀᴏᴢᴇᴛ Jan 03 '17 at 16:58

3 Answers3

1

Once you have a URL, it's as simple as a single line (and an import, to be fair):

import urllib
urllib.urlretrieve('http://example.com/somefile.gif', '/path/to/save/myfile.gif')
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • This is exactly what I tried last night, but Windows just said it couldn't open it. I will double check when I get home in a few hours. Are you sure this works with gifs? I saw an example that used this but for an image. – whatwhatwhat Jan 03 '17 at 15:01
  • 1
    @whatwhatwhat This most certainly works. If you have a specific problem with it,you'll have to share more details (such as the error you're getting) – Mureinik Jan 03 '17 at 15:05
  • **The code:** `urllib.urlretrieve(http://giphy.com/gifs/fullerhouse-3oz8xJfB6XGBwOA8HS', 'test.gif')` **The error:** `Traceback (most recent call last): File "", line 1, in AttributeError: module 'urllib' has no attribute 'urlretrieve'` – whatwhatwhat Jan 03 '17 at 22:43
  • @whatwhatwhat What python version are you using? – Mureinik Jan 03 '17 at 22:49
  • version 3.5.2 is what im using – whatwhatwhat Jan 03 '17 at 22:58
  • @whatwhatwhat you should have tagged your question with [tag:python3] - it has been moved. Try `from urllib import request; urllib.request.urlretrieve('http://example.com/somefile.gif', '/path/to/save/myfile.gif')`. – Mureinik Jan 03 '17 at 23:03
  • I'm getting this error now: `Traceback (most recent call last): File "", line 1, in NameError: name 'urllib' is not defined` – whatwhatwhat Jan 03 '17 at 23:11
  • Well, technically I got a different error *at first* but it seemed to be coming from using single quotes, so I switched to double quotes around the url and the file location. – whatwhatwhat Jan 03 '17 at 23:12
  • @whatwhatwhat arg, copy-paste error. My apologies. After the import, you should use `request.urlretrieve('http://example.com/somefile.gif'‌​, '/path/to/save/myfile.gif')`. – Mureinik Jan 03 '17 at 23:14
  • Ok so now we've reached the error I was getting last night - here is a [screenshot](http://imgur.com/a/nLVVW) – whatwhatwhat Jan 03 '17 at 23:18
  • Nvm, figured it out. – whatwhatwhat Jan 04 '17 at 00:22
0

Try this:

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

If you are not a fan of urllib, you can try using the requests module:

import requests

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

Or you also can try using a shorter alternative to the code above:

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