# 解析保存评论信息 defsave_one_page(html): comments = re.findall('(.*?)', html) for comment in comments[1:]: result = re.sub('<.>', '', comment) if'回复@'notin result: with open('comments.txt', 'a+', encoding='utf-8') as fp: fp.write(result)
爬取分析过程这里就不赘述,大家可以看代码。
数据有了,现在我们再用 Python 来看一下 TOP10 词汇有哪些,主要代码实现如下:
stop_words = [] with open('stop_words.txt'
, 'r', encoding='utf-8') as f: lines = f.readlines() for line in lines: stop_words.append(line.strip()) content = open('comments.txt', 'rb').read() # jieba 分词 word_list = jieba.cut(content) words = [] for word in word_list: if word not in stop_words: words.append(word)
wordcount = {} for word in words: if word != ' ': wordcount[word] = wordcount.get(word, 0)+1 wordtop = sorted(wordcount.items(), key=lambda x: x[1], reverse=True)[:10] wx = [] wy = [] for w in wordtop: wx.append(w[0]) wy.append(w[1])
def jieba_(): stop_words = [] with open('stop_words.txt', 'r', encoding='utf-8') as f: lines = f.readlines() for line in lines: stop_words.append(line.strip()) content = open('comments.txt', 'rb').read() # jieba 分词 word_list = jieba.cut(content) words = [] for word in word_list: if word not in stop_words: words.append(word) global word_cloud # 用逗号隔开词语 word_cloud = ','.join(words)