简体   繁体   中英

Python web scraping, getting a FileNotFound error

I am trying to run the following script to get some book data from goodreads.com starting with just a list of titles. I have had this code working recently but am now getting the following error:

Traceback (most recent call last):

File "/home/l/gDrive/AudioBookReviews/WebScraping/GoodreadsScraper.py", line 3, in reload(sys) NameError: name 'reload' is not defined

Here is the Code: https://pastebin.com/Y5NQiVEp

as the error states [Errno 2] No such file or directory

Could be permissions or your path is wrong.

The error is FileNotFoundError . Therefore, your python code is not able to find a file in your machine.

Check the path you are providing when calling open , which is this part of the error message /home/l/Downloads/WebScraping/GoodReadsBooksNew.csv .

Lets do a file existence check and access check as follows:

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+")

You need to make sure file path exists, and write the file under this directory. Make some modifications of the function 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM