爬蟲===下載圖片的方法
阿新 • • 發佈:2018-03-12
tps alt write soft lis inf post 索引 clas
方法一:
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 叠代的是元素值!
爬蟲===下載圖片的方法