Home  >  Q&A  >  body text

python - Encountered a problem when crawling images. urllib.request.urlretrieve failed to download to the specified folder?

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

py grabs the page image and saves it locally

Get page information

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

Get images through regular expressions

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

Loop to save pictures locally

html = getHtml(url_all[i])
list=getImg(html.decode())

print (url_all[1])

x = 0
for imgurl in list:
    print(x)
    filename = os.path.dirname(url_all[i])    
    filename2 = os.path.basename(filename)

os.mkdir('d:\%s'% filename2)

    local='D:\%s\%s.jpg' %(filename2,x)
    print (local)
    urllib.request.urlretrieve(imgurl,local)
    x+=1

print("done")

Execution error: (win10 64-bit system, python3.6)

File "C:Python36liburllibrequest.py", line 258, in urlretrieve

tfp = open(filename, 'wb')

FileNotFoundError: [Errno 2] No such file or directory: 'd:\46975340\0.jpg'

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.

给我你的怀抱给我你的怀抱2733 days ago1955

reply all(1)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-18 10:56:50

    Before saving, check whether the directory exists, and create it if it does not exist

    if not os.path.exists(file_path):
        os.mkdir(file_path)

    reply
    0
  • Cancelreply