Errno 2 没有这样的文件或目录“website_content.txt”

问题描述 投票:0回答:1

我遇到了一个文件未找到的错误,就是标题上的错误,我是初学者。 有人可以帮忙吗?

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()

我正在构建一个跟踪网站更改的脚本,尽最大努力避免使用

urlib
库,因为我很难理解它。如果可能的话,我想继续使用之前使用的 3 个库来解决这个问题。

python-3.x beautifulsoup python-requests
1个回答
0
投票

我猜当您第一次运行脚本时,文件不存在,因此您会收到错误。

我稍微重构一下问题:

  1. 运行时尝试加载文件内容
  2. 如果找不到文件,则返回一个虚拟字符串
  3. 定期使用此字符串检查网站内容
  4. 如果有更改,请将页面保存到文件,更新字符串并转到步骤 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)

打印:

...

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

...
© www.soinside.com 2019 - 2024. All rights reserved.