-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_urls.py
More file actions
66 lines (50 loc) · 1.93 KB
/
check_urls.py
File metadata and controls
66 lines (50 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import time
from urllib.parse import urlparse
import requests
def is_url_working(url):
try:
# Make a HEAD request to avoid downloading the body content
response = requests.head(url, timeout=5)
# Check if the response status code is 2xx or 3xx (success and redirects)
return response.status_code < 400
except requests.RequestException:
return False
def is_valid_url(url):
"""Check if the string looks like a valid URL"""
parsed_url = urlparse(url)
return bool(parsed_url.scheme and parsed_url.netloc)
def filter_working_urls(file_path, delay=1):
valid_urls = []
# Read URLs from the file
with open(file_path, "r") as file:
urls = file.readlines()
# Check each URL
for url in urls:
url = url.strip() # Clean up any newline characters
if not url or not is_valid_url(url):
print(f"Skipping invalid or empty URL: {url}")
continue # Skip invalid or empty URLs
if is_url_working(url):
print(f"URL is working: {url}")
valid_urls.append(url)
else:
print(f"URL is broken: {url}")
# Introduce a delay to avoid overwhelming the server
time.sleep(delay)
# Write valid URLs to a new file
with open("valid_urls.txt", "w") as output_file:
for valid_url in valid_urls:
output_file.write(valid_url + "\n")
print(f"Filtered {len(valid_urls)} valid URLs.")
# Prompt the user for a file name and optional delay
if __name__ == "__main__":
file_path = input("Please enter the path to the file containing URLs: ")
try:
delay = float(
input("Enter delay between requests (in seconds, default is 1 second): ")
or 1
)
except ValueError:
delay = 1 # Default to 1 second if invalid input
# Call the function with the user-provided file path and delay
filter_working_urls(file_path, delay)