import imghdr
for i in imgurl:
content = urllib2.urlopen(i).read()
imgtype = imghdr.what('', h=content)
if not imgtype:
imgtype = 'txt'
with open('F:\Download\{}.{}'.format(i, imgtype), 'wb') as f:
f.write(content)
Obviously the file name was not included when writing
The open function opens a file. The question is, it is a directory.
Just add the file name.
Obviously the file name was not included when writing
There is no downloaded file name...
Don’t use Chinese for the folder path, try changing it to an English path
Please confirm whether writing a file directly using python can be successful. The error is caused by permissions.
Replace the double quotes in the directory path with single quotes
If there are Chinese characters in the path name, it is recommended to use Unicode format so that there will be no recognition errors
urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)
This function. Simple and practical. It’s better to add exception handling