bs4 (BeautifulSoup)抓取数据,调用win.CreateObject(“SAPI.SpVoice”).Speak(‘语音播报’)
import requests
from bs4 import BeautifulSoup
import time
import json
from comtypes import client as win
# 财联社电报页面 URL
url = "https://www.cls.cn/telegraph"
def say(msg):
try:
win.CreateObject("SAPI.SpVoice").Speak(msg)
except Exception as e:
print(f"语音播报失败:{e}")
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
"Referer": "https://www.cls.cn/telegraph"
}
last_content = ""
def get_latest_telegram():
global last_content
try:
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.text, "html.parser")
script_tag = soup.find("script", {"id": "__NEXT_DATA__"})
if not script_tag:
print("未找到数据脚本")
return None
data = json.loads(script_tag.string)
# 调试用:保存原始数据
# with open("data.json", "w", encoding="utf-8") as f:
# json.dump(data, f, ensure_ascii=False, indent=2)
telegraph_list = data.get("props", {}).get("initialState", {}).get("telegraph", {}).get("telegraphList", [])
if not telegraph_list:
print("电报列表为空")
return None
latest_item = telegraph_list[0]
content = latest_item.get("content", "").replace("<br />", "\n").strip()
if content and content != last_content:
return content
except requests.exceptions.RequestException as e:
print(f"网络请求失败:{e}")
except json.JSONDecodeError as e:
print(f"JSON解析失败:{e}")
except KeyError as e:
print(f"键值不存在:{e}")
except Exception as e:
print(f"未知错误:{e}")
return None
def main():
global last_content
print("财联社电报监控已启动...")
while True:
try:
content = get_latest_telegram()
if content:
print(f"\n新电报 [{time.strftime('%H:%M:%S')}]:")
print(content)
say("最新财联社电:" + content)
last_content = content
time.sleep(60) # 正确设置为60秒
except KeyboardInterrupt:
print("\n程序已终止")
break
if __name__ == "__main__":
main()