import time
import requests
from lxml import etree
def buy_service():
print("抢购成功!")
if __name__ == '__main__':
order = False
# 设置 Telegram Bot Token
bot_token = "xxxx:AAxxxx9c" # 替换为你的 Bot Token
# 设置目标群组的 Chat ID
chat_id = "-xxxxx" # 替换为你的目标群组的 Chat ID
# 发送消息到群组
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
header = {
'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}
# 逻辑判断
while not order:
# 检测商品上货的链接 特价机的gid=60,演示中设置gid=25
response = requests.get(url='https://vps.discount/', headers=header)
html = response.content.decode('utf-8')
# 使用 lxml 解析 HTML
tree = etree.HTML(html)
# 使用 XPath 查询获取所有符合条件的 div 元素
div_elements = tree.xpath("//div[@class='main']/div[@class='main-container main-space']/div[@id='section1']/div[@class='price-list price-list-column-5']/div[@class='price-card price-card-js price-card-column-5']")
# 循环遍历每个 div 元素
for div_element in div_elements:
link_element = div_element.xpath("./div[@class='price-card-info']/a")
link = link_element[0].get("href") if link_element else ""
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
if link != "#":
# 输出链接不为 "#" 时的值
value_element = div_element.xpath("./h2")
value = value_element[0].text if value_element else ""
print(timestamp + " " + value + " 有货了")
message = timestamp + " " + value + " 有货了,购买链接"+ link
payload = {
"chat_id": chat_id,
"text": message
}
response = requests.post(url, json=payload)
# 检查请求是否成功
if response.status_code == 200:
print("消息发送成功!")
else:
print("消息发送失败!")
print(response.text)
else:
# 输出链接不为 "#" 时的值
value_element = div_element.xpath("./h2")
value = value_element[0].text if value_element else ""
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(timestamp + " " + value + " 无货")
message = timestamp + " " + value + " 无货"
payload = {
"chat_id": chat_id,
"text": message
}
response = requests.post(url, json=payload)
# 检查请求是否成功
if response.status_code == 200:
print("消息发送成功!")
else:
print("消息发送失败!")
print(response.text)
else:
order = False
time.sleep(3)
if order:
buy_service()