python 編寫一個程序 監控一個網頁是否發生變化
你可以使用Python的requests库来实现监控网页是否发生变化。以下是一个示例代码:
import requests
import hashlib
import time
# 定义要监控的网页URL
url = 'http://example.com'
# 发送请求获取网页内容
response = requests.get(url)
content = response.text
# 计算网页内容的哈希值
hash_value = hashlib.md5(content.encode()).hexdigest()
while True:
# 休眠一段时间
time.sleep(60)
# 再次发送请求获取最新的网页内容
response = requests.get(url)
content = response.text
# 计算最新的网页内容的哈希值
new_hash_value = hashlib.md5(content.encode()).hexdigest()
# 比较两次哈希值是否相同
if hash_value != new_hash_value:
print("网页内容发生变化")
# 更新哈希值为最新的值
hash_value = new_hash_value
这个程序会不断地发送请求获取网页内容,然后计算哈希值。如果两次的哈希值不同,就表示网页内容发生了变化。你可以根据实际需求进行调整,比如增加发送邮件或其他通知的功能
原文地址: https://www.cveoy.top/t/topic/hJ6Z 著作权归作者所有。请勿转载和采集!