新闻内容整理

This commit is contained in:
liuwei
2025-12-29 15:58:27 +08:00
parent 193a0350b2
commit 44daa3b650
2 changed files with 17 additions and 19 deletions

View File

@@ -66,23 +66,22 @@ class News(object):
try:
response = requests.get(url, headers=self.headers, timeout=10)
response.raise_for_status() # 检查请求是否成功
print(response.json())
response.raise_for_status()
if response.status_code == 200:
post = response.json()
# 提取content列表 - 避免使用str作为变量名
content_list = post.get('data', {}).get('cards', [])
if content_list and len(content_list) > 0:
news_items = content_list[0].get('content', [])
# 遍历列表,并格式化每个字典的title, url然后添加到output字符串中
for index, article in enumerate(news_items, start=1):
if isinstance(article, dict) and 'word' in article:
title = article['word'].replace(" ", "_")
# url = article.get('url', '')
# 使用f-string格式化字符串并添加到output中
output += f"{index} :#{title}\n"
cards = post.get('data', {}).get('cards', [])
index = 1
for card in cards:
blocks = card.get('content', [])
for block in blocks:
articles = block.get('content', [])
for article in articles:
if isinstance(article, dict) and 'word' in article:
title = str(article.get('word', '')).strip().replace(" ", "_")
raw_url = str(article.get('url', '')).strip()
url = raw_url.strip('`').strip()
output += f"{index} :#{title}\n"
index += 1
# 输出最终的字符串
return output
@@ -130,7 +129,7 @@ class News(object):
if __name__ == "__main__":
news = News()
print(news.get_news_60s())
print(news.get_baidu_news())
# # msg = "@水牛-分身 今日百度新闻"
# # q = re.sub(r"@.*?[\u2005|\s]", "", msg).replace(" ", "")
# # print(q)

View File

@@ -689,10 +689,9 @@ class Robot:
async def news_baidu_report_auto(self) -> None:
try:
# news = News().get_baidu_news()
# await self.send_group_txt_message(news, Feature.DAILY_NEWS)
news = News().get_baidu_news()
await self.send_group_txt_message(news, Feature.DAILY_NEWS)
path = News().get_news_60s()
await self.send_group_image_message(path, Feature.DAILY_NEWS)
except Exception as e:
self.LOG.error(f"newsBaiduReportAuto error{e}")