方法一:
import urllib.request
for x in range(1,21):
urllib.request.urlretrieve("http://url/"+str(x)+".jpg", ‘%s.jpg‘ % x)
方法二:
推荐
import requests
for i, j in enumerate(range(1,21)):
with open(‘{0}.jpg‘.format(i), ‘wb‘) as file:
file.write(requests.get(‘https://meiriyiwen.com/images/new_feed/bg_‘+str(j)+‘.jpg‘).content)
import requests
for i, j in enumerate(imgUrlList):
with open(‘E:/{0}.jpg‘.format(i), ‘wb‘) as file:
file.write(requests.get(j).content)
enumerate函数的功能简单解释,例如:
list_ = [‘a‘, ‘b‘, ‘c‘]
for i, j in enumerate(list_):
print([i, j])
输出结果:
[0, ‘a‘]
[1, ‘b‘]
[2, ‘c‘]
在这里 i 迭代的是list_的索引值,j 迭代的是元素值!