248 lines
		
	
	
		
			7.9 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
			
		
		
	
	
			248 lines
		
	
	
		
			7.9 KiB
		
	
	
	
		
			Markdown
		
	
	
	
	
	
# 热搜数据爬取工具
 | 
						||
 | 
						||
> 本仓库中代码仅供学习研究使用,不得用于违法用途,学习使用完毕后请于24小时内删除。
 | 
						||
>
 | 
						||
> 数据来自微博、B站(详见下方「数据来源」),本项目不对数据真实性做验证,使用数据时请遵守相关平台的相关限制要求。
 | 
						||
 | 
						||
## 简介
 | 
						||
 | 
						||
您可以将本项目代码部署在服务器上(在本地运行也可),程序会每隔一分钟拉取一次热搜数据,并保存为 `json` 格式文件。
 | 
						||
 | 
						||
 | 
						||
 | 
						||
## 数据预览
 | 
						||
 | 
						||
在部署并启动项目后,您可以在浏览器中打开 `html/index.html` 文件实时预览当前热搜。
 | 
						||
 | 
						||
 | 
						||
 | 
						||
## 数据来源
 | 
						||
 | 
						||
**微博热搜**
 | 
						||
 | 
						||
页面:https://weibo.com/hot/search
 | 
						||
 | 
						||
接口:https://weibo.com/ajax/statuses/hot_band
 | 
						||
 | 
						||
 | 
						||
 | 
						||
**B站热搜**
 | 
						||
 | 
						||
页面:https://www.bilibili.com/blackboard/activity-trending-topic.html
 | 
						||
 | 
						||
接口:https://app.bilibili.com/x/v2/search/trending/ranking
 | 
						||
 | 
						||
 | 
						||
 | 
						||
**B站排行榜**
 | 
						||
 | 
						||
页面:https://www.bilibili.com/v/popular/rank/all
 | 
						||
 | 
						||
接口:https://api.bilibili.com/x/web-interface/ranking/v2?type=all
 | 
						||
 | 
						||
(切换到其他榜单再切换回来会调用此接口)
 | 
						||
 | 
						||
 | 
						||
 | 
						||
## 运行环境
 | 
						||
 | 
						||
原理上来说 Windows 下和 Linux 都可运行,目前仅在 Windows 下测试过,暂未在 Linux 系统下测试。
 | 
						||
 | 
						||
项目使用 node 开发,以下部署流程默认您已安装了 `Git`、`Nodejs`。
 | 
						||
 | 
						||
 | 
						||
 | 
						||
## 部署
 | 
						||
 | 
						||
1. 克隆仓库(或直接下载压缩包)
 | 
						||
 | 
						||
```bash
 | 
						||
git clone https://git.only4.work/coder-xiaomo/weibo-hotband
 | 
						||
```
 | 
						||
 | 
						||
2. 安装依赖
 | 
						||
 | 
						||
```bash
 | 
						||
npm i
 | 
						||
```
 | 
						||
 | 
						||
3. 修改配置文件
 | 
						||
 | 
						||
将项目目录下的 `.env.example` 文件复制一份,并改名为 `.env`,使用文本编辑器打开(例如:记事本、VS Code、vim等均可),根据其中的注释说明来进行配置即可。
 | 
						||
 | 
						||
> 如果不创建 .env 文件,项目启动时会报如下错误并退出。
 | 
						||
>
 | 
						||
> ```bash
 | 
						||
> [ERROR] .env file not found!
 | 
						||
> ```
 | 
						||
 | 
						||
4. 启动项目
 | 
						||
 | 
						||
```bash
 | 
						||
# 直接运行
 | 
						||
# node index.js
 | 
						||
 | 
						||
# 使用 pm2
 | 
						||
# pm2 start index.js --name hotband-bot
 | 
						||
```
 | 
						||
 | 
						||
5. 停止项目
 | 
						||
 | 
						||
```bash
 | 
						||
# 使用 node index.js 命令直接运行的项目可以通过 `Ctrl + C` 停止
 | 
						||
 | 
						||
# 使用 pm2 运行的可以使用以下两行命令来停止和从列表中删除项目
 | 
						||
# pm2 stop hotband-bot
 | 
						||
# pm2 delete hotband-bot
 | 
						||
```
 | 
						||
 | 
						||
 | 
						||
 | 
						||
## 说明
 | 
						||
 | 
						||
项目爬取的数据默认保存在项目目录下的 data 文件夹中,您也可以通过修改 `.env` 文件中的 `DATA_FOLDER` 参数值来自定义数据保存路径。
 | 
						||
 | 
						||
### 微博热搜榜
 | 
						||
 | 
						||
> 微博热搜在 `weibo_hotband` 子文件夹下
 | 
						||
 | 
						||
在程序运行后,该文件夹下会出现 `latest.json` 文件及其余几个文件夹,这些子文件夹中的文件按照以下格式保存:`年/月/日/年月日_时分.json`。
 | 
						||
 | 
						||
每次爬取后,`latest.json`中的数据都会被覆盖为最新的热搜数据。
 | 
						||
 | 
						||
`origin` 文件夹中的数据是通过Api接口获取到的原始数据,没有经过任何处理。
 | 
						||
 | 
						||
<!--
 | 
						||
`simplify` 文件夹中的数据是在原始数据的基础上,去除了部分冗余数据。
 | 
						||
-->
 | 
						||
 | 
						||
`final` 文件夹中的数据是从原始数据中抽离出的有用数据,并重新整理得到的。
 | 
						||
 | 
						||
<!--
 | 
						||
`regulation` 文件夹中的数据主要用于观测原始值与显示值不同的热搜,这部分热搜猜测可能是经过微博平台调控的。(这部分数据没有太大意义,可以忽略)
 | 
						||
-->
 | 
						||
 | 
						||
 | 
						||
 | 
						||
### B站热搜榜
 | 
						||
 | 
						||
> 微博热搜在 `bilibili_hotband` 子文件夹下
 | 
						||
 | 
						||
在程序运行后,该文件夹下会出现 `latest.json` 文件及其余几个文件夹,这些子文件夹中的文件按照以下格式保存:`年/月/日/年月日_时分.json`。
 | 
						||
 | 
						||
每次爬取后,`latest.json`中的数据都会被覆盖为最新的热搜数据。
 | 
						||
 | 
						||
`origin` 文件夹中的数据是通过Api接口获取到的原始数据,此处仅仅去除了 `trackid`。
 | 
						||
 | 
						||
`final` 文件夹中的数据是从原始数据中抽离出的有用数据,并重新整理得到的。
 | 
						||
 | 
						||
 | 
						||
 | 
						||
### B站排行榜
 | 
						||
 | 
						||
> 微博热搜在 `bilibili_rank` 子文件夹下
 | 
						||
 | 
						||
在程序运行后,该文件夹下会出现 `latest.json` 文件及其余几个文件夹,这些子文件夹中的文件按照以下格式保存:`年/月/日/年月日_时分.json`。
 | 
						||
 | 
						||
每次爬取后,`latest.json`中的数据都会被覆盖为最新的热搜数据。
 | 
						||
 | 
						||
`origin` 文件夹中的数据是通过Api接口获取到的原始数据,没有经过任何处理。
 | 
						||
 | 
						||
 | 
						||
 | 
						||
## 目录结构
 | 
						||
 | 
						||
### 项目目录结构
 | 
						||
 | 
						||
```bash
 | 
						||
hotband                                      // 本项目
 | 
						||
├─ data                                      // 爬取的数据(启动项目后自动创建)
 | 
						||
├─ html                                      // html 页面
 | 
						||
│  ├─ assets
 | 
						||
│  │  ├─ css                                 // CSS 样式
 | 
						||
│  │  │  └─
 | 
						||
│  │  ├─ image                               // 前端图片资源
 | 
						||
│  │  │  ├─ ...
 | 
						||
│  │  └─ js
 | 
						||
│  │     └─ isMobile.js
 | 
						||
│  ├─ bilibili_hotband.html
 | 
						||
│  ├─ bilibili_rank.html
 | 
						||
│  └─ weibo_hotband.html
 | 
						||
├─ src                                       // 数据爬取核心代码
 | 
						||
│  ├─ utils                                  // 工具类代码
 | 
						||
│  │  ├─ fileUtils.js
 | 
						||
│  │  └─ requestUtils.js
 | 
						||
│  ├─ execute_command.js                     // 执行命令行脚本(暂时没用到)
 | 
						||
│  ├─ get_bilibili_hotband.js                // 获取 B站热搜榜 代码
 | 
						||
│  ├─ get_bilibili_rank.js                   // 爬取 B站排行榜 代码
 | 
						||
│  └─ get_weibo_hotband.js                   // 爬取 微博热搜榜 代码
 | 
						||
├─ .env.example                              // 项目配置文件模板
 | 
						||
├─ .env                                      // 项目配置文件(需要自行创建)
 | 
						||
├─ index.html                                // html 页面打开文件
 | 
						||
├─ index.js                                  // node 项目启动入口文件
 | 
						||
├─ nodemon.json
 | 
						||
├─ package-lock.json
 | 
						||
├─ package.json
 | 
						||
├─ pm2 restart.bat
 | 
						||
├─ pm2 restart.sh
 | 
						||
├─ pm2 start.bat
 | 
						||
├─ pm2 start.sh
 | 
						||
├─ pm2 stop.bat
 | 
						||
├─ pm2 stop.sh
 | 
						||
└─ README.md                                 // 项目自述文件
 | 
						||
```
 | 
						||
 | 
						||
 | 
						||
 | 
						||
### data 目录结构
 | 
						||
 | 
						||
data 文件夹下的目录结构如下
 | 
						||
 | 
						||
```bash
 | 
						||
data
 | 
						||
├─ bilibili-hotband
 | 
						||
│  ├─ final / origin
 | 
						||
│  │  └─ xxxx                             // 年
 | 
						||
│  │     └─ xx                            // 月
 | 
						||
│  │        └─ xx                         // 日
 | 
						||
│  │           ├─ xxxxxxxx_xxxx.min.json  // 年月日_时分秒.min.json
 | 
						||
│  └─ latest.json                         // 最新的json文件
 | 
						||
├─ bilibili-rank
 | 
						||
│  ├─ origin
 | 
						||
│  │  └─ xxxx                             // 年
 | 
						||
│  │     └─ xx                            // 月
 | 
						||
│  │        └─ xx                         // 日
 | 
						||
│  │           ├─ xxxxxxxx_xxxx.min.json  // 年月日_时分秒.min.json
 | 
						||
│  └─ latest.json                         // 最新的json文件
 | 
						||
└─ weibo-hotband
 | 
						||
   ├─ origin / final / simplify
 | 
						||
   │  └─ xxxx                             // 年
 | 
						||
   │     └─ xx                            // 月
 | 
						||
   │        └─ xx                         // 日
 | 
						||
   │           ├─ xxxxxxxx_xxxx.min.json  // 年月日_时分秒.min.json
 | 
						||
   ├─ regulation
 | 
						||
   │  └─ xxxx                             // 年
 | 
						||
   │     └─ xx                            // 月
 | 
						||
   │        └─ xx                         // 日
 | 
						||
   │           ├─ xxxxxxxx_xxxx.json      // 年月日_时分秒.json
 | 
						||
   └─ latest.json                         // 最新的json文件
 | 
						||
```
 | 
						||
 | 
						||
 | 
						||
 | 
						||
### 题外话:怎么生成目录结构?
 | 
						||
 | 
						||
> 有很多小伙伴在问像上方的目录结构是如何生成的,这里跟大家说下:
 | 
						||
>
 | 
						||
> 1. Windows 下可以通过 `tree` 命令来生成,例如:
 | 
						||
>
 | 
						||
>    ```bash
 | 
						||
>    tree /f > xxx.txt
 | 
						||
>    ```
 | 
						||
>
 | 
						||
> 2. 使用 VS Code 插件
 | 
						||
>
 | 
						||
>    我使用的是 [tree-generator](https://marketplace.visualstudio.com/items?itemName=xboxyan.tree-generator) 这个插件,安装之后直接在文件夹上右键即可生成
 | 
						||
>
 | 
						||
> 3. 另外还有一些其他方法也可以生成,大家可以自己探索。
 |