目录

  • 爬虫准备
  • 爬虫
  • 情绪分析

爬虫准备

本次用selenmium和bs4进行爬虫,最后把数据储存进json文件里,需要使用的库有这些。

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.chrome.options import Options
import json

爬虫

先爬取贴子的基本信息,如评论,阅读数,帖子链接等。有些咨询帖子是官方发的,和爬取的股票没有关系,所以需要过滤掉。

def get_list(stock, x, y):DFCF_dict = {'view_num': [], 'comment_num': [], 'title': [], 'poster': [], 'latest_time': [], 'sub_url': []}href_list = []total_list = []print('从第{}页到第{}页'.format(x, y - 1))for i in range(x, y):print('正在第{}页'.format(i))chrome_options = Options()chrome_options.add_argument('--headless')browser = webdriver.Chrome(options=chrome_options)url = 'http://guba.eastmoney.com/list,{}_{}.html'.format(stock, i)url2 = 'http://guba.eastmoney.com'browser.get(url)html_source = browser.page_sourcesoup = BeautifulSoup(html_source, "html.parser")view_num = soup.find_all('span', class_='l1 a1')comment_num = soup.find_all('span', class_='l2 a2')title = soup.find_all('span', class_='l3 a3')poster = soup.find_all('span', class_='l4 a4')time = soup.find_all('span', class_='l5 a5')for element in view_num[1:]:DFCF_dict['view_num'].append(element.string)for element in comment_num[1:]:DFCF_dict['comment_num'].append(element.text)for element in title[1:]:a = element.find('a')href = a.get('href')title = a.get('title')href_list.append(href)DFCF_dict['title'].append(title)for element in poster[1:]:DFCF_dict['poster'].append(element.text)for element in time[1:]:DFCF_dict['latest_time'].append(element.text)for i in range(len(time[1:])):if (href_list[i][:5] == '/news') and href_list[i][7].isdigit():sub_url = url2 + href_list[i]else:sub_url = ''DFCF_dict['sub_url'].append(sub_url)browser.close()print('结束爬虫')return DFCF_dict

这个函数是为了爬取帖子内部的评论等信息,可以翻页。

def get_comment(sub_url):comment_list = []comment_time_list = []sub_comment_list = []sub_comment_time_list = []chrome_options = Options()chrome_options.add_argument('--headless')browser = webdriver.Chrome(options=chrome_options)browser.get(sub_url)html_source = browser.page_sourcesoup = BeautifulSoup(html_source, "html.parser")post_time = soup.find('div', class_="zwfbtime")article = soup.find('div', class_="stockcodec .xeditor").textarticle_like = soup.find_all('div', id='like_wrap')[0].textcomment = soup.find_all('div', class_='full_text')comment_time = soup.find_all('div', class_='publish_time')sub_comment = soup.find_all('span', class_="l2_full_text")sub_comment_time = soup.find_all('span', class_='time fl')page_num = soup.find_all('span', class_='sumpage')if len(page_num) == 0:page_num = 1else:page_num = int(page_num[0].text)print('本贴共{}页:'.format(page_num))for element in comment:comment_list.append(element.text)for element in comment_time:comment_time_list.append(element.text)for element in sub_comment:sub_comment_list.append(element.text)for element in sub_comment_time:sub_comment_time_list.append(element.text)print('爬取第1页')browser.close()if page_num > 1:for i in range(2, page_num + 1):print('爬取第{}页'.format(i))new_url = sub_url[:-5] + '_{}'.format(i) + sub_url[-5:]browser = webdriver.Chrome(options=chrome_options)browser.get(new_url)html_source = browser.page_sourcesoup = BeautifulSoup(html_source, "html.parser")comment = soup.find_all('div', class_='full_text')comment_time = soup.find_all('div', class_='publish_time')[4:]sub_comment = soup.find_all('span', class_="l2_full_text")sub_comment_time = soup.find_all('span', class_='time fl')[4:]for element in comment:comment_list.append(element.text)for element in comment_time:comment_time_list.append(element.text)for element in sub_comment:sub_comment_list.append(element.text)for element in sub_comment_time:sub_comment_time_list.append(element.text)browser.close()return article, article_like, comment_list, comment_time_list, sub_comment_list, sub_comment_time_list

最后的爬虫程序是这样的

if __name__ == '__main__':DFCF_dict = get_data_from_DFCF(600000,1,3)for url in DFCF_dict['sub_url']:if url != '':article,article_like,comment_list,comment_time_list,sub_comment_list,sub_comment_time_list = get_comment(url)DFCF_dict['article'].append(article)DFCF_dict['article_like'].append(article_like)DFCF_dict['comment'].append(comment_list)DFCF_dict['comment_time'].append(comment_time_list)DFCF_dict['sub_comment'].append(sub_comment_list)DFCF_dict['sub_comment_time'].append(sub_comment_time_list)DFCF_json = json.dumps(DFCF_dict,sort_keys=False, indent=4, separators=(',', ': '))f = open('data.json', 'w')f.write(DFCF_json)

情绪分析

用snownlp对评论进行分析

import json
import pandas as pd
from snownlp import SnowNLP

因为电脑配置爬不了太多数据,所以只是个简单版

all_comment = []
all_comment_time = []
for comment in info_data['comment']:for i in range(len(comment)):all_comment.append(comment[i])for comment in info_data['sub_comment']:for i in range(len(comment)):all_comment.append(comment[i])for time in info_data['comment_time']:for i in range(len(time)):all_comment_time.append(time[i][4:])for time in info_data['sub_comment_list']:for i in range(len(time)):all_comment_time.append(time[i])df1 = pd.DataFrame([all_comment_time,all_comment])
df1 = df1.T
df1.columns = ['date','comment']
df1['date'] = pd.to_datetime(df1['date'])
df1.set_index(['date'], inplace=True)
df1['view'] = 0.0
for i in range(len(df1)):npl = SnowNLP(df1.comment[i])df1.view[i] = npl.sentiments * 100df1.plot.bar()

东方财富股吧评论爬虫和情绪分析相关推荐

  1. 爬虫(一):东方财富股吧评论

    ** 爬虫(一):用Python获取东方财富股吧评论 ** 第一次尝试爬虫,因为需要选择了东方财富股吧,需要的小伙伴可以一起来学习,首先声明一下,本人也是从b站学来了,第一次就是照葫芦画瓢,有样学样获 ...

  2. 利用python爬取东方财富网股吧评论并进行情感分析(一)

    利用python爬取东方财富网股吧评论(一) python-东方财富网贴吧文本数据爬取 分享一下写论文时爬数据用到的代码,有什么问题或者改善的建议的话小伙伴们一起评论区讨论.涉及内容在前人的研究基础之 ...

  3. 爬取东方财富股吧评论

    前言 前天有人要我帮忙爬取一下东方财富网几只股票的股票吧评论,晚上花了点时间给他弄了一下,很简单啊 一.分析网站 打开东方财富网,网址很简单:600340这只股票评论一共1303页,每页的网址如下: ...

  4. 网易云音乐“王牌冤家”用户评论:评论爬虫及情感分析(SnowNLP)

    李荣浩上周发的<耳朵>专辑,你萌听了吗?小编真的超级喜欢"王牌冤家"这首歌,在新说唱听了李老师的那几句就一直念念不忘,这一周可是一直单曲循环中.恰好前两天看了SnowN ...

  5. 基于大数据的情绪分析(二)

    导言 情绪分析使用机器学习算法来确定正面或负面文本内容的方式.情绪分析的示例包括: 快速了解客户评论的基调: 了解客户喜欢或不喜欢的产品或服务. 了解可能影响新客户购买决策的因素. 为企业提供市场意识 ...

  6. python爬取股票评论_Python爬虫股票评论,snowNLP简单分析股民用户情绪

    原标题:Python爬虫股票评论,snowNLP简单分析股民用户情绪 一.背景 股民是网络用户的一大群体,他们的网络情绪在一定程度上反映了该股票的情况,也反映了股市市场的波动情况.作为一只时间充裕的研 ...

  7. 东方财富股吧标题爬取分析

    45个股吧,140万条数据库记录 日期从2018-03-01至2021-03-01共36个月的股吧帖子, 爬取股吧名称.阅读.评论.标题.作者和发帖时间, 并分析总体情绪 亮点回顾 时间问题 获取的时 ...

  8. python商品评论数据采集与分析可视化系统 Flask框架 requests爬虫 NLP情感分析 毕业设计 源码

    一.项目介绍 python商品评论数据采集与分析可视化系统 Flask框架.MySQL数据库. requests爬虫.可抓取指定商品评论.Echarts可视化.评论多维度分析.NLP情感分析.LDA主 ...

  9. 如何科学地蹭热点:用python爬虫获取热门微博评论并进行情感分析

    前言:本文主要涉及知识点包括新浪微博爬虫.python对数据库的简单读写.简单的列表数据去重.简单的自然语言处理(snowNLP模块.机器学习).适合有一定编程基础,并对python有所了解的盆友阅读 ...

  10. 金融作业:股吧评论_爬取、情绪与股价对比(贵州茅台和上证指数)

    效果图(情绪与股价) 文件夹"上证指数吧--股评文本情感分析"爬取了40W+条股吧--上证指数吧的股评文本数据,并通过分析这些股评文本积极.消极情况,计算每天的情绪指数(BI_in ...

最新文章

  1. Flex布局教程(来源:阮一峰)
  2. 常见被病毒利用的漏洞补丁
  3. Shell(3)——截取某些字符、默认值处理
  4. Python 同一个类中不同函数相互调用
  5. element tabs 添加事件_JavaScript-跨浏览器事件处理程序-Web前端教程
  6. 查看、启动、关闭防火墙
  7. JSP获取浏览者真实IP地址方法
  8. ASP.NET 实践:锁定 ASP.NET 配置设定
  9. java怎么给类中的私有变量赋值_java练习本(原每日一练)(20190430)
  10. Talib技术因子详解(五)
  11. php 省份的缩写,34个省级行政区记忆口诀、省份简称和省会城市表
  12. 在安卓模拟器(mumu为例)上联调app并且用Charles抓包
  13. 西门子G120变频器初始化参数设置
  14. 微软逆转互联网战局,错过了智能手机却君临游戏帝国
  15. 在线遥感影像与地图集数据下载搜集
  16. MATLAB算法实战应用案例精讲-【数据分析】时序异常检测(补充篇)(附Java、R语言和python代码实现)
  17. Spring Boot Redis 实现分布式锁,真香!!
  18. linux中read函数的用法,Linux中read命令的简介及使用方法
  19. Java将下划线大写方式命名的字符串转换为驼峰式
  20. Clickhouse 在唯品会数据产品的实践

热门文章

  1. 华师计算机考研英语过线,华师大学长:考研英语我是如何考到77分的!
  2. py使用pie绘制饼图或圆环图
  3. 泰然金融全国用户见面会走进豫陕,与用户零距离对话
  4. 计算机科学技术的广告语,让人动心的十大经典IT广告语
  5. nano板载电脑连接无线时断时续
  6. 基于信息熵的新词发现算法
  7. 命令行修改微信小程序开发AppId
  8. RAR压缩包密码如何解密
  9. linux中tmp文件在哪,学习LINUX入门,/tmp文件夹
  10. Ambisonics声音格式及麦克