Python爬虫持续监控商品价格

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秒重新获取一次价格

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值