我写了一个机器人,检查是否有任何变化,在一个给定的网站,因为上次被刮。为此,它会抓取网站,将其html代码存储在本地文件中,然后一遍又一遍地抓取,如果新版本和旧版本之间存在差异,它会覆盖本地文件并打印“已触发”。问题是,我的脚本总是找到差异,并覆盖文件,即使没有任何更改
可复制示例:
import requests
import time
import os
def compare(file, url):
if os.path.isfile("./" + file):
scrape = requests.get(url).text
with open(file) as f:
txt=f.read()
if not txt == scrape:
with open(file, "w") as f:
f.write(scrape)
print("Triggered")
else:
scrape=requests.get(url).text
with open(file, "w") as f:
f.write(scrape)
ceu = "https://hro.ceu.edu/find-job"
ceu_file = "ceu.html"
while True:
compare(ceu, ceu_file)
time.sleep(10)
所以,问题是脚本每次抓取网站时都会被触发,即使网站不会每10秒更改一次。为什么函数中的txt==scrape
总是false,从而触发脚本
您需要通过设置
newline=''
来禁用自动换行符转换,以防止在写入文件时换行符转换为系统默认值:相关问题 更多 >
编程相关推荐