爬取文本文档提取关键字显示文中关键词出现的次数
时间: 2024-05-01 21:16:04 浏览: 23
实现这个功能需要用到以下步骤:
1. 读取文本文件内容
2. 对文本进行分词处理,得到词语列表
3. 去除停用词(如“的”、“了”等),得到关键词列表
4. 统计关键词列表中每个关键词出现的次数
5. 显示统计结果
下面是一个简单的Python代码实现:
```python
import jieba
from collections import Counter
# 读取文件内容
with open('test.txt', 'r', encoding='utf-8') as f:
content = f.read()
# 分词处理
words = list(jieba.cut(content))
# 去除停用词
stopwords = ['的', '了', '是', '我', '你', '他', '她']
keywords = [word for word in words if word not in stopwords]
# 统计关键词出现次数
counter = Counter(keywords)
# 显示统计结果
for word, count in counter.most_common():
print(word, count)
```
这个代码使用了jieba库进行中文分词,并使用了collections库中的Counter类对关键词进行统计。可以根据需要调整停用词列表和统计结果的显示方式。
相关问题
java爬取微博的热点关键字
以下是使用Java爬取微博热点关键字的示例代码:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class WeiboHotKeywordsCrawler {
private static final String WEIBO_HOT_KEYWORDS_URL = "https://s.weibo.com/top/summary?cate=realtimehot";
public static void main(String[] args) {
try {
List<String> hotKeywords = getWeiboHotKeywords();
System.out.println(hotKeywords);
} catch (IOException e) {
e.printStackTrace();
}
}
public static List<String> getWeiboHotKeywords() throws IOException {
Document doc = Jsoup.connect(WEIBO_HOT_KEYWORDS_URL).get();
Element tbody = doc.selectFirst("tbody");
Elements trs = tbody.select("tr");
List<String> hotKeywords = new ArrayList<>();
for (Element tr : trs) {
Element a = tr.selectFirst("a");
String keyword = a.text();
hotKeywords.add(keyword);
}
return hotKeywords;
}
}
该代码使用Jsoup库连接到微博热点关键字页面并获取关键字列表。该页面包含一个表格,其中每行都包含一个关键字和相关的热度数据。我们选择表格的主体元素,然后遍历每一行并提取第一个链接元素的文本,即关键字。最后,我们将关键字添加到列表中并返回列表。
python根据关键字爬取文本信息
你可以使用 Python 的爬虫库(如 requests、BeautifulSoup)来爬取网页,再使用关键字过滤出需要的文本信息。以下是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 定义要爬取的网页 URL 和关键字
url = 'https://www.example.com'
keyword = 'Python'
# 发送 GET 请求并获取网页 HTML
response = requests.get(url)
html = response.text
# 使用 BeautifulSoup 解析 HTML
soup = BeautifulSoup(html, 'html.parser')
# 找到所有文本节点并过滤出包含关键字的节点
text_nodes = soup.find_all(text=True)
keyword_nodes = [node for node in text_nodes if keyword in node]
# 输出结果
for node in keyword_nodes:
print(node)
```
上述代码会爬取指定网页并输出其中包含关键字的文本节点。你可以根据需要修改代码,例如通过循环爬取多个网页、使用正则表达式进行更复杂的关键字匹配等。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)