Python Forum

Full Version: Why I am not getting the Redirected codes using requests?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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."""
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})"
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:

# 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!")
Thank you all. I have found the reason! By default I see the requests.get follows the redirected URLs and takes the status code 200! That is why I did not get the 300 codes!! I have disabled the redirect following by "allow_redirects=False"

So, the line number 6 of code was
response = requests.get(url, timeout=5)
and I changed to
response = requests.get(url, timeout=5,allow_redirects=False)
Please keep two things in mind in the future.

1. Post code in code tags (Python Button).
2. Full quotes are pointless.