Skip to content

Instantly share code, notes, and snippets.

@grantwinney
Last active June 20, 2018 17:12
Show Gist options
  • Save grantwinney/db7446197a0df1373349c49b851b6f37 to your computer and use it in GitHub Desktop.
Save grantwinney/db7446197a0df1373349c49b851b6f37 to your computer and use it in GitHub Desktop.
Reads in a list of URLs (from a CSV file), writes any redirected URLs to a new CSV file, and prints out missing (404) URLs to the console
import csv
import urllib2
with open('urls.csv', 'rb') as f:
reader = csv.reader(f)
with open('urls-output.csv', 'w') as g:
writer = csv.writer(g, delimiter=',')
for row in reader:
url = row[0]
try:
r = urllib2.urlopen(url)
except urllib2.URLError as e:
r = e
if r.code == 200 and url != r.geturl():
writer.writerow([url, r.geturl()])
elif r.code == 404:
print("Not found: " + r.geturl())
@grantwinney
Copy link
Author

grantwinney commented Jun 20, 2018

I use this to update mappings between Ghost (blog) and Disqus (comments). They seem to get out of whack, either because Disqus maps to the GUID version of the URL when I preview a draft, or because I change the title after its been published and Disqus can't handle that.

After downloading a URL map file from Disqus, rename the file to "urls.csv". It should be a single-column file with one URL per row.

Copy this Python script into the same directory and run it, and it should create a "urls-output.csv" file with two columns. The first column is the original URL and the second is the redirected URL, like this:

https://grantwinney.com/p/02a61540-f5ac-4bfc-aa9f-94751b426df9/,https://grantwinney.com/day-12-openweathermap-api/
https://grantwinney.com/p/03cfb59f-1c2e-44b8-af94-001ca3601d04/,https://grantwinney.com/day-9-hubblesite-api/
https://grantwinney.com/p/071e2a91-0e5d-403b-bca4-762362da4671/,https://grantwinney.com/weekend-quotes-5/
https://grantwinney.com/p/08603435-be75-4e59-a5b8-e030c9c76db4/,https://grantwinney.com/day-13-us-census-bureau-api/

Two of the URLs in the first code block are missing from the second because they no longer exist and returned a 404. If trying to retrieve a URL returns a 404, it's printed to the console. If it returns a 200 OK and the URL wasn't redirected (same as original), the script just moves on to the next one without writing to the file or printing to the console.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment