import os import re import requests from bs4 import BeautifulSoup from PIL import Image…
- User
import os import re import requests from bs4 import BeautifulSoup from PIL import Image from io import BytesIO from urllib.parse import urljoin from tqdm import tqdm import zipfile import time import random
Constants
BASE_URL = "https://hdri-haven.com" HDRI_URL_PATTERN = re.compile(r"https://hdri-haven\.com/hdri/[^/]+")
Directories
DOWNLOAD_DIR = "downloads"
def create_directories(): os.makedirs(DOWNLOAD_DIR, exist_ok=True)
def get_all_hdri_pages(): """ Scans the BASE_URL to find all HDRI page URLs matching the pattern. """ response = requests.get(BASE_URL) response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser')
links = set() for a_tag in soup.find_all('a', href=True): href = urljoin(BASE_URL, a_tag['href']) if HDRI_URL_PATTERN.match(href): links.add(href) return list(links)def get_2k_image_link(hdri_page_url): """ Scrapes the 2K image download link from an HDRI page. """ response = requests.get(hdri_page_url) response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser')
link_2k = soup.find('a', class_='downloadable link-2k', href=True) if link_2k: return link_2k['href'] return Nonedef download_image(image_url): """ Downloads an image from the given URL and saves it to the DOWNLOAD_DIR. Returns the file path of the downloaded image. """ local_filename = os.path.join(DOWNLOAD_DIR, os.path.basename(image_url)) with requests.get(image_url, stream=True) as r: r.raise_for_status() with open(local_filename, 'wb') as f: for chunk in r.iter_content(chunk_size=8192): f.write(chunk) return local_filename
create_directories()
print("Scanning website for HDRI pages...") hdri_pages = get_all_hdri_pages() print(f"Found {len(hdri_pages)} HDRI pages.")
downloaded_images = [] for page in tqdm(hdri_pages, desc="Processing HDRI pages"): image_link = get_2k_image_link(page) if image_link: try: image_path = download_image(image_link) downloaded_images.append(image_path) # Random delay between 2 to 3 seconds time.sleep(random.uniform(2, 3)) except Exception as e: print(f"Failed to download {image_link}: {e}")
hello this can download only 48 images there is a lot more on my website why this can be happening find a solution?
- Assistant