Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Tải ảnh Google Images với Python (google_images_download)

Thư viện google_images_download cho phép tìm kiếm và tải ảnh từ Google Images tự động qua từ khóa.

Cài đặt

>>> pip install google_images_download

Ví dụ cơ bản

from google_images_download import google_images_download

response = google_images_download.googleimagesdownload()

arguments = {
    "keywords": "lilly,hills",  # Từ khóa, cách nhau bởi dấu phẩy
    "limit": 5,               # Số ảnh tối đa mỗi từ khóa
    "print_urls": True        # In URL ảnh tải về
}

paths = response.download(arguments)
print(paths)

Kết quả (rút gọn)

Image URL: https://assets.traveltriangle.com/.../Hill-Stations-Near-Kolkata-cover1-400x267.jpg
Completed Image ====> 4.Hill-Stations-Near-Kolkata-cover1-400x267.jpg
...
{'lilly': ['C:\\downloads\\lilly\\1.Lilly-Tougas.jpg', ...],
 'hills': ['C:\\downloads\\hills\\1.220px-Clouds_over_hills.jpg', ...]}

Các tham số quan trọng

Lưu ý quan trọng

  • Google có thể chặn request tự động (rate limiting, CAPTCHA)
  • Tuân thủ robots.txt và Terms of Service của Google
  • Chỉ dùng cho mục đích học tập/nghiên cứu, không dùng thương mại
  • Thư viện này không còn được maintain tích cực (archived)

Thay thế hiện đại

Dùng requests + BeautifulSoup hoặc selenium để tự xây crawler, hoặc API chính thức (Google Custom Search API - có phí).

Tham sốMô tả
keywordsTừ khóa tìm kiếm (bắt buộc)
limitSố lượng ảnh (mặc định: 100)
output_directoryThư mục lưu (mặc định: downloads/)
image_formatĐịnh dạng: jpg, png, gif...
sizeKích thước: large, medium, icon...
aspect_ratioTỷ lệ: tall, square, wide, panoramic
chromedriverĐường dẫn ChromeDriver (cần cho một số trường hợp)