There is no problem if you download to the D drive, but there is a problem when downloading to the directory I created (mainly because I want to create a directory on the D drive named with the number in front of the question mark in the URL, such as (http://v .yupoo.com/photos/196...') is not possible because there are many links and the number of each link is different. I want to use this number as the name of the folder to store the pictures downloaded from this link. )
The source code is as follows:
import urllib.request
import re
import os
url_all =['http://v.yupoo.com/photos/196...',
'http://v.yupoo.com/photos/196...',
'http://v.yupoo.com/photos/196...',
'http://v.yupoo.com/photos/196...',]
def getHtml(url):
html = urllib.request.urlopen(url).read()
return html
def getImg(html):
reg = 'src="(.+?\.jpg)"'
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
# print(imglist)
return imglist
for i in range(len(url_all)):
html = getHtml(url_all[i])
list=getImg(html.decode())
x = 0
for imgurl in list:
print(x)
filename = os.path.dirname(url_all[i])
filename2 = os.path.basename(filename)
local='D:\%s\%s.jpg' %(filename2,x)
print (local)
urllib.request.urlretrieve(imgurl,local)
x+=1
Execution error: (win10 64-bit system, python3.6)
File "C:Python36liburllibrequest.py", line 258, in urlretrieve
tfp = open(filename, 'wb')
After testing
The last sentence written like this can be output: urllib.request.urlretrieve(imgurl,'d:\%s.jpg'% str(i*10 x))
After testing, the first two sentences are ok, add the third sentence:
local='d:\%s\%s.jpg' %(filename2,x)
print (local)
urllib.request.urlretrieve(imgurl,local)
The error message is as follows: (Same as above)
File "C:Python36liburllibrequest.py", line 258, in urlretrieve
tfp = open(filename, 'wb')
FileNotFoundError: [Errno 2] No such file or directory: 'd:\46975340\0.jpg'
Could you please tell me, is there any problem with this path? How should it be written.
Before saving, check whether the directory exists, and create it if it does not exist