Search code examples
python-3.xbeautifulsouppython-requests

Errno 2 no such file or directory "website_content.txt"


I'm gettin a file not found error, the one on the title, and I'm a beginner. Can someone help?

import requests
import time
from bs4 import BeautifulSoup

Headers = {"user-agent":"my user-agent"}

URL = "www.myurl.com"

def monitor_website_changes():
    while True:
        r = requests.get(URL, headers=Headers)
        with open("website_content.txt", "r") as file:
            previous_content = file.read()
        if response.txt == previous_content:
            print("website content has changed")
        with open("website_content.txt", "w") as file:
            file.write(response.txt)
        time.sleep(1)

monitor_website_changes()

I'm building a script that tracks website changes, trying my very best to avoid the urlib library because of my difficulties understanding it. If it's possible I would like to solve the problem sticking to the 3 libraries utilized before.


Solution

  • I guess when you run the script for first time the file not exists so you get the error.

    I restructure the problem a little bit:

    1. when run try to load the file content
    2. if the file is not found, return a dummy string
    3. periodically check the website content with this string
    4. if there's change, save the page to file, update string and goto step 3.
    import time
    
    import requests
    
    
    def get_prev_content(filename):
        try:
            with open(filename, "r", encoding="utf-8") as f_in:
                return f_in.read()
        except FileNotFoundError:
            return "dummy"
    
    
    def monitor(filename, url):
        prev_content = get_prev_content(filename)
        while True:
            time.sleep(2)
    
            resp = requests.get(url)
            if prev_content == resp.text:
                print(".")
                continue
    
            print(f"{url=} changed, writing to {filename=} !")
    
            with open(filename, "w", encoding="utf-8") as f_out:
                f_out.write(resp.text)
    
            prev_content = resp.text
    
    
    url = "https://news.ycombinator.com/"
    filename = "website_content.txt"
    
    monitor(filename, url)
    

    Prints:

    ...
    
    .                
    .                                              
    url='https://news.ycombinator.com/' changed, writing to filename='website_content.txt' !
    .
    
    ...