Hi, thank you for spending your valuable time to help me. I have tried the following code to get the dead, redirected, alive URLs. But I am not getting the redirected URL response code, example
I am sure about 302 code status from the browsers network view but not getting by the code. I even have specified instead of a range but did not work. My code records status code 200 instead of 302! Please help.
import csv
import requests
def check_url_status(url):
"""Checks the status of a URL and returns a descriptive status string."""
try:
response = requests.get(url, timeout=5)
status_code = response.status_code
if status_code in range(200, 299):
return f"Alive ({status_code})"
elif status_code == 300:
return f"Multiple Choices Redirected ({status_code})"
elif status_code == 301:
return f"Moved Permanently Redirected ({status_code})"
elif status_code == 302:
return f"Temporarily Moved Redirected ({status_code})"
elif status_code in range(303, 400):
return f"Redirected ({status_code})"
else:
return f"Not Found ({status_code})"
except Exception as e:
return f"Error: {e}"
# Read URLs from CSV file
urls = []
with open("urls.csv", "r") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
urls.append(row[0])
# Check each URL and write results to new file
with open("url_status.csv", "w", newline="") as outfile:
writer = csv.writer(outfile)
writer.writerow(["URL", "Status"])
for url in urls:
status = check_url_status(url)
writer.writerow([url, status])
print("URL status successfully written to url_status.csv!")
I am sure about 302 code status from the browsers network view but not getting by the code. I even have specified instead of a range but did not work. My code records status code 200 instead of 302! Please help.
import csv
import requests
def check_url_status(url):
"""Checks the status of a URL and returns a descriptive status string."""
try:
response = requests.get(url, timeout=5)
status_code = response.status_code
if status_code in range(200, 299):
return f"Alive ({status_code})"
elif status_code == 300:
return f"Multiple Choices Redirected ({status_code})"
elif status_code == 301:
return f"Moved Permanently Redirected ({status_code})"
elif status_code == 302:
return f"Temporarily Moved Redirected ({status_code})"
elif status_code in range(303, 400):
return f"Redirected ({status_code})"
else:
return f"Not Found ({status_code})"
except Exception as e:
return f"Error: {e}"
# Read URLs from CSV file
urls = []
with open("urls.csv", "r") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
urls.append(row[0])
# Check each URL and write results to new file
with open("url_status.csv", "w", newline="") as outfile:
writer = csv.writer(outfile)
writer.writerow(["URL", "Status"])
for url in urls:
status = check_url_status(url)
writer.writerow([url, status])
print("URL status successfully written to url_status.csv!")
Larz60+ write Dec-21-2023, 05:59 PM:
removed spam link
removed spam link