সরাসরি প্রধান সামগ্রীতে চলে যান

Download Pexels Images Using API | Save Any Image with Python | API Guide

 I've found the API of pexels, which might work for a while...
Is it allowed???
Sample code:




Python code

# Install required libraries !pip install requests import os import requests from urllib.parse import urljoin # Create the directory to save images save_dir = "dwnImgs" os.makedirs(save_dir, exist_ok=True) # Function to fetch image data from Pexels API def fetch_images(query, page=1, per_page=10): url = "https://www.pexels.com/en-us/api/v3/search/photos" headers = { "accept": "*/*", "accept-language": "en-BD,en;q=0.9,bn-BD;q=0.8,bn;q=0.7,en-GB;q=0.6,en-US;q=0.5", "authorization": "", "content-type": "application/json", "priority": "u=1, i", "sec-ch-ua": "\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Google Chrome\";v=\"126\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Windows\"", "sec-fetch-dest": "empty", "sec-fetch-mode": "cors", "sec-fetch-site": "same-origin", "secret-key": "I've Intentionally hid it!", } params = { "query": query, "page": page, "per_page": per_page, "orientation": "all", "size": "all", "color": "all", "sort": "popular", "seo_tags": "true" } response = requests.get(url, headers=headers, params=params) if response.status_code == 200: return response.json() else: print(f"Failed to fetch data: {response.status_code} - {response.text}") return None # Function to download images def download_images(data): if not data or "data" not in data: print("No data available to process.") return for item in data["data"]: image_url = item["attributes"]["image"]["download_link"] image_id = item["id"] image_filename = os.path.join(save_dir, f"{image_id}.jpg") # Download the image response = requests.get(image_url, stream=True) if response.status_code == 200: with open(image_filename, "wb") as img_file: img_file.write(response.content) print(f"Saved image: {image_filename}") else: print(f"Failed to download image: {image_url}") # Fetch and download images query = "lawn mower" # Change this query to your desired search term page = 1 per_page = 5 # Number of images per page image_data = fetch_images(query=query, page=page, per_page=per_page) download_images(image_data)




I've successful! Any query you give, any quantity image you want and get it without any hassle!
No need to visit pexels!








মন্তব্যসমূহ

এই ব্লগটি থেকে জনপ্রিয় পোস্টগুলি