热点话题检测python代码
时间: 2023-11-08 08:53:50 浏览: 120
以下是一个简单的热点话题检测的Python代码示例:
```
import requests
from bs4 import BeautifulSoup
def get_news_titles(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
titles = []
for headline in soup.find_all(['h1', 'h2']):
title = headline.get_text().strip()
if len(title) > 0:
titles.append(title)
return titles
def detect_hot_topics(url):
titles = get_news_titles(url)
hot_topics = []
keywords = ['COVID', 'pandemic', 'vaccine', 'election', 'Brexit', 'climate change']
for title in titles:
for keyword in keywords:
if keyword.lower() in title.lower():
hot_topics.append(title)
break
return hot_topics
url = 'https://www.bbc.com/news'
hot_topics = detect_hot_topics(url)
print('Hot topics:')
for topic in hot_topics:
print(' - ' + topic)
```
此代码使用了 `requests` 和 `BeautifulSoup` 库来获取并解析指定新闻网站的页面,然后搜索标题中是否包含热门话题的关键字(如COVID、选举等),如果包含则将其添加到热门话题列表中并输出。请注意,此代码仅是一个简单示例,实际情况可能需要更复杂的算法和技术来识别热门话题。
阅读全文