-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDo the imgur links still work.py
39 lines (33 loc) · 1.32 KB
/
Do the imgur links still work.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import requests
import csv
from urllib.parse import urlparse, parse_qs
# Create the CSV header
with open('DoesItStillExistImgur.csv', 'w', newline='') as csvfile:
fieldnames = ['Imgur_ID', 'Status']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
# Loop through each line in urls.txt
with open('urls.txt', 'r') as f:
for line in f:
line = line.strip()
# Extract the Imgur ID from the URL
parsed_url = urlparse(line)
imgur_id = parsed_url.path.split('/')[-1].split('.')[0]
# Check if the URL exists without downloading it
try:
response = requests.head(line, allow_redirects=False)
status_code = response.status_code
# Check the exit status of the request
if status_code == 200:
# URL exists
status = 'Succeeds'
else:
# URL does not exist
status = 'Not'
except requests.RequestException:
# URL does not exist or other error occurred
status = 'Not'
# Write to CSV
with open('DoesItStillExistImgur.csv', 'a', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow({'Imgur_ID': imgur_id, 'Status': status})