Python Web抓取,出现FileNotFound错误

时间:2019-09-01 01:45:07

标签: python web-scraping

我正在尝试运行以下脚本,以从goodreads.com处获取一些书数据,这些书数据仅以书名列表开头。我最近有运行此代码,但是现在出现以下错误:

Traceback (most recent call last):

文件“ /home/l/gDrive/AudioBookReviews/WebScraping/GoodreadsScraper.py”,第3行,在     重新加载(sys) NameError:名称“ reload”未定义

以下是代码:https://pastebin.com/Y5NQiVEp

4 个答案:

答案 0 :(得分:2)

因为错误状态[Errno 2]没有这样的文件或目录

可能是权限或您的路径错误。

答案 1 :(得分:2)

错误为FileNotFoundError。因此,您的python代码无法在您的计算机中找到文件。

检查调用open时提供的路径,这是错误消息/home/l/Downloads/WebScraping/GoodReadsBooksNew.csv的一部分。

答案 2 :(得分:2)

让我们进行文件存在性检查和访问检查,如下所示:

import os

filePathStr = '/home/l/Downloads/WebScraping/GoodReadsBooksNew.csv'
if os.path.isfile(): 
    if os.access(filePathStr, os.R_OK):
        print("File exists and is readable")
        fileHandle = open(filePathStr, "w+")
    else:
        print("ERROR: File exists and is NOT readable")
else:
    print("Creating output file "+filePathStr)
    fileHandle = open(filePathStr, "w+")

答案 3 :(得分:0)

您需要确保文件路径存在,并将文件写入此目录下。对函数create_csv_file()进行一些修改。

import os
def create_csv_file():
    header = ['Title', 'URL']
    directory = '/home/l/Downloads/WebScraping'
    if not os.path.exists(directory):
        os.makedirs(directory)
    with open(os.path.join(directory,'GoodReadsBooksNew.csv'), 'w') as csv_file:
        wr = csv.writer(csv_file, delimiter=',')
        wr.writerow(header)