Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrmeyerhofer/2234946ac1ae43b5630d1c32aa35bd6b to your computer and use it in GitHub Desktop.
Save jrmeyerhofer/2234946ac1ae43b5630d1c32aa35bd6b to your computer and use it in GitHub Desktop.
import sys
import requests
import csv
import time
time.sleep(3)
data = []
## Open the file with read only permit
f = open('viaf.txt')
## Read the first line
line = f.readline()
## If the file is not empty keep reading line one at a time
## till the file is empty
while line:
newurl = line
r = requests.get(line, allow_redirects=False, timeout=0.9)
if 300 <= r.status_code < 400:
newurl = r.headers['location'] #print r.headers['location']
#else:
# newurl = line #print line
# save the data in tuple
data.append((newurl))
print newurl
time.sleep(3)
line = f.readline()
f.close()
# open a csv file with append, so old data will not be erased
with open('viaf.csv', 'a') as csv_file:
writer = csv.writer(csv_file, csv.QUOTE_NONE)
# The for loop
for newurl in data:
writer.writerow([newurl])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment