我已经设置了一个 API,它在 csv 文件的列中返回 pdf 链接。现在我需要依次下载这些 pdf 文档。但是它返回 IsADirectoryError: [Errno 21] Is a directory: 当有空白行或 html 而不是 pdf 链接并停止时。解决方法是什么?谢谢。
我正在使用此代码。
import os
import csv
import requests
write_path = '</pathname>' # ASSUMING THAT FOLDER EXISTS!
with open('</**.csv>', 'r') as csvfile:
os.listdir(write_path)) if os.path.isfile(i)]
spamreader = csv.reader(csvfile)
for link in spamreader:
print('-'*72)
pdf_file = link[4].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[4]))
a = requests.get(link[4], stream=True)
for block in a.iter_content(512):
if not block:
break
pdf.write(block)
print('OK.')
except requests.exceptions.RequestException as e:
print('REQUESTS ERROR:')
print(e)