Python请求给出错误:IndexError:列表索引超出范围

时间:2017-05-17 07:03:12

标签: python web-scraping python-requests

Python请求给出错误: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

尝试使用python中的请求包下载1000+ pdf文件。

Traceback (most recent call last):
  File "update.py", line 11, in <module>
    pdf_file = link[0].split('/')[-1] 
IndexError: list index out of range

Error

1 个答案:

答案 0 :(得分:1)

csv文件中可能有一些空行。在这种情况下,link将是空字符串'',您将收到索引错误。将代码更改为:

.
.
.
with open('final.csv', 'r') as csvfile:
    spamreader = csv.reader(csvfile)
    for link in spamreader:
        if not link:
            continue
        print('-'*72)
        pdf_file = link[0].split('/')[-1]
.
.
.

进一步说明;你的代码似乎奇怪地缩进了。目前,它只会打开final.csv中的最后一个pdf。您确定不希望缩进第二个with语句,以及在for循环中执行的其他代码(还有一个级别)吗?