import os import requests from bs4 import BeautifulSoup import sys def download_images(url): session = requests.Session() session.proxies = { 'http': 'socks5h://127.0.0.1:9050', 'https': 'socks5h://127.0.0.1:9050', } try: response = session.get(url) response.raise_for_status() # Check for HTTP errors soup = BeautifulSoup(response.text, 'html.parser') # Find all images with the specified structure images = soup.find_all('img', loading='lazy') if not os.path.exists('downloaded_images'): os.makedirs('downloaded_images') for img in images: thumb_url = img.get('src') full_image_url = img.find_parent('a').get('href') # Get the href from the parent tag if full_image_url: # Construct the complete URLs full_image_url = requests.compat.urljoin(url, full_image_url) img_response = session.get(full_image_url) img_response.raise_for_status() # Check for HTTP errors img_name = os.path.join('downloaded_images', full_image_url.split('/')[-1]) with open(img_name, 'wb') as f: f.write(img_response.content) print(f'Downloaded: {img_name}') except requests.exceptions.RequestException as e: print(f"An error occurred: {e}") if __name__ == "__main__": if len(sys.argv) != 2: print("Usage: python download_images.py ") sys.exit(1) url = sys.argv[1] download_images(url)