新闻内容整理

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

View File

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