Created
July 24, 2025 22:08
-
-
Save AshtonIzmev/e2c490475fc77d928feb768a6b81c30c to your computer and use it in GitHub Desktop.
Download links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import os | |
| import time | |
| from urllib.parse import urlparse | |
| from cvs import cvs | |
| # Fake user agent to mimic a real browser | |
| headers = { | |
| 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' | |
| } | |
| # Directory where files will be saved | |
| download_directory = "downloaded_files" | |
| # Create the download directory if it doesn't exist | |
| if not os.path.exists(download_directory): | |
| os.makedirs(download_directory) | |
| print(f"Created directory: {download_directory}") | |
| print( | |
| f"Attempting to download files to: {os.path.abspath(download_directory)}") | |
| for url in cvs: | |
| try: | |
| # Parse the URL to get the filename | |
| parsed_url = urlparse(url) | |
| # Get the last part of the path, which should be the filename | |
| filename = os.path.basename(parsed_url.path) | |
| # If the filename is empty (e.g., URL ends with a slash), try to generate one | |
| if not filename: | |
| # A simple fallback: use a hash or a generic name | |
| filename = f"downloaded_file_{abs(hash(url))}.pdf" | |
| print( | |
| f"Warning: Could not determine filename from URL '{url}'. Using '{filename}'." | |
| ) | |
| file_path = os.path.join(download_directory, filename) | |
| # Check if the file already exists | |
| if os.path.exists(file_path): | |
| print( | |
| f"Skipping: File '{filename}' already exists at '{file_path}'." | |
| ) | |
| continue | |
| print(f"Downloading: {url} to {file_path}") | |
| # Send a GET request to the URL with SSL verification disabled for problematic certificates | |
| response = requests.get(url, stream=True, verify=False, headers=headers) | |
| response.raise_for_status( | |
| ) # Raise an HTTPError for bad responses (4xx or 5xx) | |
| # Open the file in binary write mode and save the content | |
| with open(file_path, 'wb') as f: | |
| for chunk in response.iter_content(chunk_size=8192): | |
| f.write(chunk) | |
| print(f"Successfully downloaded: {filename}") | |
| except requests.exceptions.SSLError as e: | |
| print(f"SSL Certificate error downloading {url}: {e}") | |
| except requests.exceptions.ConnectionError as e: | |
| print(f"Connection error downloading {url}: {e}") | |
| except requests.exceptions.RequestException as e: | |
| print(f"Request error downloading {url}: {e}") | |
| except Exception as e: | |
| print(f"An unexpected error occurred for {url}: {e}") | |
| # Add a 1-second timeout between requests | |
| time.sleep(1) | |
| print("\nDownload process completed.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment