Thì sẵn tiện thì mình dùng JSON luôn nhé.
Đoạn source code Python như sau:
# Importing required libraries
import urllib.request
import json
import os
# Adding information about user agent
opener=urllib.request.build_opener()
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
urllib.request.install_opener(opener)
f = open('data.json',)
data = json.load(f)
output_dir = 'images'
# Make output directory if not exist
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# save path
for i in data['data']:
filename = i.split("/")[-1]
image_save_path = output_dir + '/' + os.path.basename(i)
# calling urlretrieve function to get resource
urllib.request.urlretrieve(i, image_save_path)
print(image_save_path)
f.close()
Trong code trên mình sẽ mở file sau đó là truy cập vào biến và lấy liên kết ảnh.
Sau đó get name ảnh và cho lưu vào thư mục .
Vậy là xong lưu lại và chạy thôi.
À quên cấu trúc json mình đang sử dụng là
{
"data": [
"link ảnh 1",
"link ảnh 2",
"link ảnh 3".
........
]
}