Skip to content

Instantly share code, notes, and snippets.

@jimmyctk
Created February 21, 2024 08:26
Show Gist options
  • Save jimmyctk/920f4067bfc1ce28c2143202765be020 to your computer and use it in GitHub Desktop.
Save jimmyctk/920f4067bfc1ce28c2143202765be020 to your computer and use it in GitHub Desktop.
Python get the final redirected URL from CSV
import csv
import requests
# Open the input CSV file
with open("location_import.csv", "r") as input_file:
# Create a CSV reader object
reader = csv.DictReader(input_file)
# Open the output CSV file
with open("location_export.csv", "w") as output_file:
# Create a CSV writer object
writer = csv.DictWriter(output_file, fieldnames=reader.fieldnames + ["finalUrl"])
# Write the header row
writer.writeheader()
# Loop through each row in the input file
for row in reader:
# Get the original link from the url field
original_link = row["url"]
# Get the final link using requests
final_link = requests.get(original_link).url
# Update the row with the final link
row["finalUrl"] = final_link
# Write the row to the output file
writer.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment