Created
February 21, 2024 08:26
-
-
Save jimmyctk/920f4067bfc1ce28c2143202765be020 to your computer and use it in GitHub Desktop.
Python get the final redirected URL from CSV
This file contains 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 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