如何利用Python监控别人的网站
import requests
from bs4 import BeautifulSoup
import smtplib
from email.mime.text import MIMEText
# 网站 URL
url = "https://"
# 发送邮件的参数
sender = '你的发件人邮箱'
receiver = '你的收件人邮箱'
smtp_server = 'smtp.xxx.com' # 发件人邮箱的 SMTP 服务器地址
smtp_port = 465 # 发件人邮箱的 SMTP 端口
username = '你的发件人邮箱'
password = '你的发件人邮箱密码'
def send_email(subject, body):
# 创建 MIMEText 邮件
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
# 发送邮件
with smtplib.SMTP_SSL(smtp_server, smtp_port) as server:
server.login(username, password)
server.sendmail(sender, receiver, msg.as_string())
def get_product_info(product_url):
# 获取商品信息
response = requests.get(product_url)
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.find('h1', class_='h3').text
price = soup.find('span', class_='h2').text
stock = soup.find('span', class_='js-product-stock').text
return (title, price, stock)
def check_product_sold_out(product_url):
# 检查商品是否已售出
response = requests.get(product_url)
soup = BeautifulSoup(response.text, 'html.parser')
if soup.find('div', class_='product-sold-out'):
return True
else:
return False
# 定期检查商品
while True:
# 获取网页内容
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 查找商品列表
product_list = soup.find('div', class_='js-product-list')
# 检查每个商品是否售出
for product in product_list.find_all('a', class_='product-card'):
product_url = product['href']
product_title = product.find('h2').text
if check_product_sold_out(product_url):
# 商品已售出,发送邮件通知
subject = f'商品已售出:{product_title}'
body = f'商品名称:{product_title}
'
send_email(subject, body)
else:
# 商品未售出,检查是否为新商品
try:
# 尝试获取商品信息,如果获取失败则说明是新商品
product_title, product_price, product_stock = get_product_info(product_url)
except:
# 新商品,发送邮件通知
subject = f'新商品上架:{product_title}'
body = f'商品名称:{product_title}
库存:{product_stock}
价格:{product_price}
'
send_email(subject, body)
# 等待一段时间后再次检查
time.sleep(300)