Python requests giving error: IndexError: list index out of range :
import os
import csv
import requests
write_path = '/Users/specter/Desktop/pdfs/u' # ASSUMING THAT FOLDER EXISTS!
with open('final.csv', 'r') as csvfile:
spamreader = csv.reader(csvfile)
for link in spamreader:
print('-'*72)
pdf_file = link[0].split('/')[-1]
with open(os.path.join(write_path, pdf_file), 'wb') as pdf:
try:
# Try to request PDF from URL
print('TRYING {}...'.format(link[0]))
a = requests.get(link[0], stream=True)
for block in a.iter_content(512):
if not block:
break
pdf.write(block)
print('OK.')
except requests.exceptions.RequestException as e: # This will catch ONLY Requests exceptions
print('REQUESTS ERROR:')
print(e) # This should tell you more details about the error
While trying to download 1000+ pdf's files using request package in python.
Traceback (most recent call last):
File "update.py", line 11, in <module>
pdf_file = link[0].split('/')[-1]
IndexError: list index out of range

link. It seems, that yourlinkvariable is probably empty. Have you checked the contents?link? Maybe with a simpleprint(link)before the line with thelink[0].split?linkis an empty sequence.