简体   繁体   中英

Python 3.4.3 save image from url to file using urllib

I tried to make a python program that would allow me to download a jpg file from a website.

Why I'm doing this is really for no reason at all, I just wanted to try it for fun.

Anyways, here is the code:

import urllib

a = 1

while a == 1:

    urllib.urlretrieve("http://lemerg.com/data/wallpapers/38/957049.jpg","D:\\Users\\Elias\\Desktop\\FolderName-957049.jpg")

(You may have to properly tab it in, it wouldn't let me here)

So basically what I want it to do is to repeatedly download the same file until I close the program. Just don't ask why.

The error code I get is: Traceback (most recent call last): urllib.urlretrieve(" http://lemerg.com/data/wallpapers/38/957049.jpg ","D:\\Users\\Elias\\Desktop\\FolderName-957049.jpg") AttributeError: 'module' object has no attribute 'urlretrieve'

urlretrieve() in Python3 is in the urllib.request module. Do this:

from urllib import request

a = 1

while a == 1:

    request.urlretrieve("http://lemerg.com/data/wallpapers/38/957049.jpg","D:\\Users\\Elias\\Desktop\\FolderName-957049.jpg")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM