Created
January 27, 2018 19:43
-
-
Save jrmeyerhofer/2234946ac1ae43b5630d1c32aa35bd6b to your computer and use it in GitHub Desktop.
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 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