简体   繁体   中英

Downloading Images with Urllib in Python 3.6.4

I ran the following code to download an image using urllib module. But I ended up facing some errors as below.

import urllib.request
import random

def downloader(image_url):    
    file_name = random.randrange(1,10000)    
    full_file_name = str(file_name) + '.jpg'    
    urllib.request.urlretrieve(image_url, full_file_name)    
    url = input("URL ")   
    downloader(url)

Error Message:

File "img_down.py", line 1, in import urllib.request File "C:\\Users\\shekh\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\urllib\\request.py", line 88, in import http.client File "C:\\Users\\shekh\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\http\\client.py", line 71, in import email.parser File "C:\\Users\\shekh\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\email\\parser.py", line 12, in from email.feedparser import FeedParser, BytesFeedParser File "C:\\Users\\shekh\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\email\\feedparser.py", line 27, in from email._policybase import compat32 File "C:\\Users\\shekh\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\email_policybase.py", line 7, in from email import header File "C:\\Users\\shekh\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\email\\header.py", line 16, in import email.quoprimime File "C:\\Users\\shekh\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\email\\quoprimime.py", line 44, in from string import ascii_letters, digits, hexdigits ImportError: cannot import name 'ascii_letters'

there should be file named string.py in your current directory, renaming it should fix the problem.

From the docs:

When a module named spam is imported, the interpreter searches for a file named spam.py in the current directory, and then in the list of directories specified by the environment variable PYTHONPATH. This has the same syntax as the shell variable PATH, that is, a list of directory names

so when one of your dependencies try to import string, program first look in the current directory and then in PYTHONPATH

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