pip install newspaper3k python import newspaper from newspaper import Article import numpy as np from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.cluster import KMeans python urls = ['https://example.com/article1', 'https://example.com/article2', 'https://example.com/article3'] python def get_article_text(url): article.download() article.parse() return article.text python articles_text = [] for url in urls: text = get_article_text(url) articles_text.append(text) python vectorizer = TfidfVectorizer() tfidf_matrix = vectorizer.fit_transform(articles_text) cosine_similarity = (tfidf_matrix * tfidf_matrix.T).A python kmeans = KMeans(n_clusters=k) kmeans.fit(tfidf_matrix) python groups = [[] for _ in range(k)] for i, label in enumerate(kmeans.labels_): groups[label].append(urls[i]) python for i, group in enumerate(groups): print(f"Cluster {i+1}:") for url in group: print(url) print(' ')


上一篇:
下一篇:
切换中文