import requests
from bs4 import BeautifulSoup
import time
# 设置要监控的商品页面URL和价格阈值
url = 'https://item.taobao.com/item.htm?id=123456789' # 替换成您要监控的商品页面URL
threshold_price = 100.0 # 替换成您要设定的价格阈值
# 定义一个函数,用于获取商品价格并返回该价格(如果价格未更改,则返回None)
def get_price():
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
price = soup.find('em', {'class': 'tb-rmb-num'}).text.strip()
price = float(price)
return price
except Exception as e:
print('获取价格时发生错误:', e)
return None
# 开始监控
while True:
price = get_price()
if price is not None:
if price <= threshold_price:
print('价格已经达到或低于您的设定阈值!')
# 在此添加发送通知的代码
break
else:
print('价格还未达到您的设定阈值,当前价格为:', price)
time.sleep(60) # 每隔60秒重新获取一次价格
Python爬虫持续监控商品价格
最新推荐文章于 2024-11-28 11:01:47 发布