import requests from bs4 import BeautifulSoup import os import logging import re from urllib.parse import urlparse, parse_qs # Set up logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') def download_image(url, card_name, card_type): folder = f"downloaded_images/{card_type}" if not os.path.exists(folder): os.makedirs(folder) image_name = f"{card_name}.png" image_path = os.path.join(folder, image_name) try: with requests.get(url, stream=True) as r: r.raise_for_status() with open(image_path, 'wb') as f: for chunk in r.iter_content(chunk_size=8192): f.write(chunk) logging.info(f"Image downloaded: {image_path}") except requests.RequestException as e: logging.error(f"Error downloading {url}: {e}") def get_image_urls_and_names(url): headers = {'User-Agent': 'Mozilla/5.0'} images_info = [] card_type = parse_qs(urlparse(url).query).get('card_type', ['unknown'])[0] # Extract card type from URL try: response = requests.get(url, headers=headers) response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser') images = soup.find_all('img') for img in images: if 'src' in img.attrs: src = img['src'] # Assuming the card name can be extracted from the image src or alt attribute card_name_match = re.search(r'/([^/]+?)(-\d+x\d+)?\.\w+$', src) if card_name_match: card_name = card_name_match.group(1).replace('b2-', '').replace('-308x420', '').replace('_', ' ') images_info.append((src, card_name, card_type)) logging.info(f"Found {len(images_info)} images for card type {card_type}.") return images_info except requests.RequestException as e: logging.error(f"Request error: {e}") except Exception as e: logging.error(f"An error occurred: {e}") urls = [ "https://foursouls.com/card-search/?card_type=character", "https://foursouls.com/card-search/page/2/?card_type=character", "https://foursouls.com/card-search/?card_type=eternal", "https://foursouls.com/card-search/page/2/?card_type=eternal", "https://foursouls.com/card-search/?card_type=treasure", "https://foursouls.com/card-search/page/2/?card_type=treasure", "https://foursouls.com/card-search/page/3/?card_type=treasure", "https://foursouls.com/card-search/page/4/?card_type=treasure", "https://foursouls.com/card-search/page/5/?card_type=treasure", "https://foursouls.com/card-search/page/6/?card_type=treasure", "https://foursouls.com/card-search/?card_type=loot", "https://foursouls.com/card-search/page/2/?card_type=loot", "https://foursouls.com/card-search/page/3/?card_type=loot", "https://foursouls.com/card-search/?card_type=monster", "https://foursouls.com/card-search/page/2/?card_type=monster", "https://foursouls.com/card-search/page/3/?card_type=monster", "https://foursouls.com/card-search/page/4/?card_type=monster", "https://foursouls.com/card-search/page/5/?card_type=monster", "https://foursouls.com/card-search/page/6/?card_type=monster", "https://foursouls.com/card-search/page/7/?card_type=monster", "https://foursouls.com/card-search/?card_type=bsoul", "https://foursouls.com/card-search/?card_type=room", "https://foursouls.com/card-search/page/2/?card_type=room" ] all_images_info = [] for url in urls: images_info = get_image_urls_and_names(url) if images_info: all_images_info.extend(images_info) if all_images_info: for image_url, card_name, card_type in all_images_info: download_image(image_url, card_name, card_type) else: logging.info("No image URLs found or an error occurred.")