Giter Site home page Giter Site logo

pubsenti-finder's Introduction

pubsenti-finder

项目介绍

使用爬虫周期性地爬取微博热搜榜,获取热搜列表,每条热搜内的热门微博和每条微博的评论。使用文本分类算法对每条评论进行情感分析,将所有微博数据和情感分析的结果存储在Redis里。运行Web服务直观地展示微博数据和情感分析结果。

Web界面: Web界面

项目结构介绍

util包

HttpUtil类负责发送http请求。RedisUtil类负责使用Jedis连接Redis服务器并返回Jedis实例读写数据到Redis。SegmentUtil类重写了分词方法。

entity包

Hot类、Weibo类和Comment类分别表示热搜、微博和评论的实体类,它们存储了基本的元数据和进行情感分析后的加工数据。

crawler包

CrawlTask类继承了TimerTask能够周期性的运行,对微博热搜榜进爬取,进行情感分析,然后将数据存储到Redis。具体的爬取任务由WeiboParser类负责,由于微博页面基本都是动态的,请求微博相应的接口返回Json数据,然后使用jackson框架解析微博元数据。

classifier包

Train类读取语料文件,存储到内存里,调用HanLP对文本进行分词处理,并计算每个词的卡方值,得到显著的词作为特征。最后生成Model类的模型。Model使用朴素贝叶斯算法进行文本分类。

MyClassifier类组合了TrainModel类,优先从指定的路径加载序列化的模型文件,如果未获取到模型就使用Train重新根据语料计算模型,然后将计算得到的模型序列化到指定路径。MyClassifier类的getScore方法调用模型对文本进行情感分析,所以实际使用中只会用到MyClassifier

其实HanLP已经实现了一个分本算法,参考HanLPClassifer类。

web包

Web服务由Spring Boot支持,后端从Redis读取数据(其实数据可以直接保存在内存的数据结构里,因为前期设计时考虑对多次爬取的数据进行横向分析才存使用Redis管理数据,为了方便后续扩展还是继续使用Redis),将数据渲染到Thyemeleaf模版中然后返回html。

模版存储在src/main/resources/templates目录下,前端使用Bootstrap编写, 图表使用chart.js渲染。

运行环境

  • Java 8以上,本项目使用了一些Java 8的特性;
  • Maven,下载项目依赖的包;
  • Redis,某些数据存储在Redis中;

运行准备

1.设置Redis服务器地址

找到RedisUtil,修改ADDR为你服务器的ip地址,如果Redis服务器设置了密码就将AUTH改为你的密码。

2.设置HTTP请求参数

由于微博的限制,未登录的账户只能获取到第一页微博评论。为了爬取更多评论你需要在浏览器中登录m.weibo.cn,浏览微博评论列表,然后打开浏览器的开发者工具查看你发送的hotflow?id=...请求(这是请求评论request)里的Cookie,将其复制到HttpUtil类里的header("cookie", "")的第二个参数里。

你还可以将user-agent设置成自己浏览器的user-agent标识。

3.下载分词所需的数据

本项目使用的分词工具为HanLP,为了分词更准确它需要一些额外的数据。

下载data.zip,解压后将data目录复制到src/main/resources目录下即可(分词只用到data/dictionary目录,可以将data/model目录删除节省空间),HanLP会自动在classpath下面找到数据。

4.设置模型及语料的路径

我提供了一些基本的语料作为参考,在src/main/resources/train目录下。

复制模型weibo-model的绝对路径到MyClassifier类的MODEL_PATH注意一定要用绝对路径,后续路径也是,因为项目打包成jar后运行的classpath路径不确定,相对路径可能会失效),运行时如果模型文件存在就不用重新训练模型。

复制停用词表cn_stopwords.txt、否定词表cn_nonwords.txt和自定义词库customwords.txt的路径到SegmentUtil类的相应变量。

如果想要使用其他的语料训练模型,可以复制积极情绪语料pos.txt和消极情绪语料neg.txt的绝对路径到Train类相应的路径。

运行

  • 下载了pox.xml中依赖的包后,在IDE中直接运行Application类中的main函数。
  • 或者使用mvn clean package命令将项目打包成jar,使用java -jar *.jar运行。

看控制台的输出,爬完一次微博后在浏览器打开http://localhost:8080查看Web页面。

自定义参数

由于微博翻爬虫系统对爬虫的限制非常严重,所以我设置的爬取速度非常慢,每次爬取的微博数量比较少,如果你解决了微博反爬问题,可以自定义这些参数提高爬取效率。

  • Application类设置Timerperiod参数调整定时爬虫任务的周期(默认30分钟运行一次);
  • CrawlTask类设置一次爬取热搜榜上的热搜个数HOT_LIST_SIZE(默认为10条),每条热搜下微博个数WB_LIST_SIZE(默认为一条),每条微博评论的页数CM_LIST_SIZE(默认为5页,一页20条评论);
  • CrawlTask类设置每次爬取的数据存在Redis中的过期时间KEY_EXPIRE_TIME(默认一小时后过期)。

pubsenti-finder's People

Contributors

dependabot[bot] avatar marwincn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pubsenti-finder's Issues

JsonParseException: Unexpected character ('<' (code 60)):

您好,我在运行您的代码时,爬取微博的时候每条都出现了这样的异常:err然后在网页中图表和评论列表会一直重复出现,网页变得老长老长,请问您有遇到过这样的异常吗?是怎样解决的呢?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.