Notification texts go here. Buy Now!

Hưỡng dẫn download ảnh Hàng Loạt bằng Python

 

Hưỡng dẫn download ảnh Hàng Loạt bằng Python

Hello xin chào các bạn lại là mình đây. Thì hôm nay mình sẽ hưỡng dẫn cho các bạn cách download ảnh hàng loạt bằng python nhé. Lý do xuất hiện bài này là do mình đang ngồi tìm cách download 4k3 ảnh từ một file json nào đó, nên sẵn tiện viết lại chia sẻ cho các bạn nào cần nhé. Pro thì xin tha cho em ạ.

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()
Cấu trúc json mình đang sử dụng là:
{
	"data": [
    "link ảnh 1",
    "link ảnh 2",
    "link ảnh 3".
    ........
    ]
}
Trong code trên mình sẽ mở file data.Json sau đó là truy cập vào biến data và lấy liên kết ảnh.
Sau đó get name ảnh và cho lưu vào thư mục images.
Vậy là xong lưu lại và chạy thôi.

Nguồn : Quang Sáng Blog
Được Chia Sẻ Lại Bởi : NguyenThanhQuoc.Site

About the Author

Chia sẻ, học hỏi, kết nối và thành công...

Đăng nhận xét

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.