如何用python统计html文件中单词出现的次数

如题,请问如何用python统计本地html文件中每个单词出现的次数?尝试过with open() 配合read(),但效果不好,统计出的结果少于单词实际出现次数

我当时是这样统计的:

freq_count = {}  # store the count of each word

with open(file_path, 'r+', encoding='utf-8') as document:
    tokens = document.read().split()

    for token in tokens:
        token = re.sub(r'\W+', '', token)
        token = token.lower()

        # update dict
        if token not in freq_count:
            freq_count[token] = 1
        else:
            freq_count[token] += 1

要统计字数的文件类似于:https://en.wikipedia.org/wiki/Social_intelligence
已存为本地HTML文件

先提取html中的文本

参考:https://matix.io/extract-text-from-webpage-using-beautifulsoup-and-python/

然后再split计数

问题不在于with open() 和read,在于你读取后怎么解析和统计的,以及你的html是什么内容
贴出来代码和html才好帮你分析原因。

好比你说喝水用杯子感觉水味道不好,用什么别的容器。但是杯子没问题,要检查你的水源是否污染,是一个道理。