从包含python中的链接的txt下载图像

时间:2018-04-06 07:07:13

标签: python urllib

import urllib.request
import socket
socket.setdefaulttimeout(30)
c = 0
f = open('urls.txt','r')
links = f.readlines()
r = len(links)
print(r)
for i in range(len(links)):
    links[i] = links[i][:-1]


try:
    for i in range(r):
        urllib.request.urlretrieve(links[i],'dosa\{}.jpg'.format(c))
        c+=1
        print("done downloading {}.jpg".format(c))
except:
    pass
f.close()

我编写了上面的代码来下载一组URL,这些图像的URL位于文本文件中。 它包含400个链接,但下载在25次下载后停止

400
done downloading 1.jpg
done downloading 2.jpg
done downloading 3.jpg
done downloading 4.jpg
done downloading 5.jpg
done downloading 6.jpg
done downloading 7.jpg
done downloading 8.jpg
done downloading 9.jpg
done downloading 10.jpg
done downloading 11.jpg
done downloading 12.jpg
done downloading 13.jpg
done downloading 14.jpg
done downloading 15.jpg
done downloading 16.jpg
done downloading 17.jpg
done downloading 18.jpg
done downloading 19.jpg
done downloading 20.jpg
done downloading 21.jpg
done downloading 22.jpg
done downloading 23.jpg
done downloading 24.jpg
done downloading 25.jpg

我还尝试在第24次下载后添加一个继续,但似乎没有任何效果。

异常会出现此错误:

HTTP Error 403: Forbidden

0 个答案:

没有答案