|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 不二如是 于 2021-12-20 18:06 编辑
在线视频:
年底的瓜有点猛吖,吃不过来了,上代码帮忙!
我们先找到微博源头出处:(传送门)
文中透漏的完全是一副好聚好散,岁月静好的气氛,好像用词有点不当了,不过也不纠结这些了。
这个评论量,哪里看的完,不数据分析一下,可能会漏掉一些精彩地方~
实现原理其实和小师妹之前做电影分析的教程一毛一样,来不急讲了,不会的自己去看~
迫不及待撸起代码了:
- # 爬取一页评论内容
- def get_one_page(url):
- headers = {
- 'User-agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3880.4 Safari/537.36',
- 'Host' : 'weibo.cn',
- 'Accept' : 'application/json, text/plain, */*',
- 'Accept-Language' : 'zh-CN,zh;q=0.9',
- 'Accept-Encoding' : 'gzip, deflate, br',
- 'Cookie' : '自己的Cookie',
- 'DNT' : '1',
- 'Connection' : 'keep-alive'
- }
- # 获取网页 html
- response = requests.get(url, headers = headers, verify=False)
- # 爬取成功
- if response.status_code == 200:
- # 返回值为 html 文档,传入到解析函数当中
- return response.text
- return None
复制代码
记住 get_one_page(url) 中,Cookie 要换成你们自己的!
数据有了,来看下前十的词汇有哪些,主要代码实现如下:
- 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])
- (
复制代码
再来生成个词云,看看:
不做评论,让数据自己说话,这个瓜,好吃吗?
源码:
FihsC-ChiGua.zip
(5.09 KB, 下载次数: 46, 售价: 8 鱼币)
|
评分
-
查看全部评分
|