简介:python3爬取新浪微博数据,js前端可视化
作者:毛怡伟、梁家坤
cnpm install
npm start分析维度:全部博主的男女对比
分析方式:散点图
分析内容:
- 男女的占比;
- 男女的平均微博数对比;
- 男女的平均粉丝数对比
分析维度:全部博主地域分布
分析方式:地图
分析内容:
- 微博使用人数在全国的分布情况,普遍在东部颜色较深;
- 各省微博使用的人数
分析维度:微博数、关注数、粉丝数
分析方式:雷达图
分析内容:比较 全部博主的平均数 与 特定博主 的情况
分析维度:好友圈、好友拓扑
分析方式:关系图
分析内容:
-
博主的直接、间接好友的朋友有哪些人
-
博主的好友中男女分布
分析维度:微博打卡情况
分析方式:日历图
分析内容:
-
挖掘特定博主在一年内的微博发布情况
-
日历图单元格内的点越大,表示当天微博数量越多
-
点击后还能看到当天所有微博出现的高频、关键词
分析维度:微博热度趋势
分析方式:折线图
分析内容:
-
博主微博的点赞、评论、转发数量的变化趋势;
-
每条微博的内容
分析维度:微博常用词
分析方式:词云图
分析内容:
-
博主的常用词分布,大小表示频度
-
博主的关注领域
分析维度:微博发布时间
分析方式:饼图、散点图
分析内容:
- 博主在周几的发布频率
- 博主在几点的发布频率
- 博主在周几的几点发布频率
./src
├── assets # 图片素材
│ ├── boy.png
│ ├── girl.png
├── global.css # 全局css
├── layouts
│ ├── index.css # 整部布局css
│ └── index.js # 整体布局
├── menuConfig.js # 左侧菜单配置
├── models # Dva Models
│ └── blogger.js # blogger数据的Dva Model
├── pages # 存取各个页面
│ ├── BloggerDistribution
│ │ └── index.js # 博主男女分布可视化
│ ├── bloggers
│ │ ├── Components
│ │ │ ├── BasicInfo.js # 粉丝数、关注数、信息可视化
│ │ │ ├── FriendsTopology.js # 朋友关系拓扑图
│ │ │ ├── PopularityTrend.js # 热度趋势折现图
│ │ │ ├── WeiboCalendar.js # 微博发布日历
│ │ │ ├── WeiboPublishTime.js # 微博发布时间
│ │ │ └── WeiboWordCloud.js # 词云图
│ │ └── $name.js # 博主详情页
│ ├── HeatMap
│ │ └── index.js # 博主地理位置热力图
│ ├── index.css # 首页css样式
│ └── index.js # 首页(博主列表)
├── services
│ └── bloggerService.js # 后端数据接口
└── utils
├── config.js # 项目配置
└── request.js # 数据请求功能的封装
./server
├── index.js # 程序入口
├── lib
│ ├── mongo.js # 连接mongodb
│ └── utils.js # 工具函数
├── models
│ ├── bloggerFriend.js # 博主朋友关系Model
│ ├── blogger.js # 博主Model
│ ├── weiboContent.js # 微博内容Model
└── routers
├── bloggers.js # 博主路由
└── statistic.js # 统计数据路由
路由:GET /api/bloggers
请求参数:
- page:第几页
- limit:每页几个
- name:博主姓名模糊搜索
返回数据:
[
{
"_id": "5c2f2381e5286e5170d6fa9d",
"id": "5878659096",
"name": "超话社区",
"sex": "男",
"birthplace": "北京",
"weibo_num": 2500,
"following": 1044,
"followers": 75880204
}
]路由:GET /api/bloggers/count
请求参数:
- name:博主姓名模糊搜索
返回数据:
{
"count": 10500
}路由:GET /api/bloggers/:id
请求参数:
- id:博主id
返回数据:
{
"_id": "5c2f2381e5286e5170d6fa9d",
"id": "5878659096",
"name": "超话社区",
"sex": "男",
"birthplace": "北京",
"weibo_num": 2500,
"following": 1044,
"followers": 75880204
}路由:GET /api/bloggers/:id/friends
请求参数:
- id:博主id
返回数据:
[
{
"_id": "5c3830e23a0d0501b2b15f9d",
"friend_id": {
"_id": "5c2f5492e5286e1020d8ddf9",
"id": "5136362277",
"friends": [
{
"_id": "5c382e0c3a0d0501b2b0e63b",
"friend_id": {
"_id": "5c327921e5286e5de011e5bc",
"id": "5574029246",
"friends": [
{
"_id": "5c3831743a0d0501b2b17fa0",
"friend_id": "5c36154be5286e47bc26014a",
"id": "6540424837",
"name": "种草小海龟",
"followers": 2707217,
"sex": "女",
"birthplace": "广东"
}
],
"birthplace": "北京",
"followers": 2454415,
"name": "微博电商",
"sex": "男"
},
"id": "5574029246",
"name": "微博电商",
"followers": 2454415,
"sex": "男",
"birthplace": "北京"
},
],
"birthplace": "北京",
"followers": 16449291,
"name": "微博红包",
"sex": "男"
},
"id": "5136362277",
"name": "微博红包",
"followers": 16449291,
"sex": "男",
"birthplace": "北京"
}
]路由:GET /api/bloggers/:id/weiboContent
请求参数:
- id:博主id
返回数据:
{
"contents": [
{
"tfidf": [
{
"_id": "5c3aa6c6338845026949e1a8",
"word": "鲁豫有约",
"weight": 27.801355304
}
],
"publish_content": "在@鲁豫有约 做过至少三次对话,鲁豫既有传统媒体人的责任跟态度,又能接收新鲜文化跟内容,每次都聊的很畅快#鲁豫有约18岁了#生日快乐",
"publish_location": "无",
"publish_time": "2019-01-05 15:01",
"like": 438,
"forward": 65,
"comment": 116,
"publish_tool": "iPhone 7"
}
],
"tfidf": [
{
"word": "AI",
"weight": 1056.5283876375188
},
{
"word": "偷笑",
"weight": 503.9408293195
},
]
}路由:GET /api/statistic/average
返回数据:
{
"_id": null,
"avg_followers": 1954959.6494285713,
"avg_weibo_num": 6631.755142857143,
"avg_following": 512.3624761904762,
"max_followers": 182775632,
"max_weibo_num": 756859,
"max_following": 20000
}路由:GET /api/statistic/sex
返回数据:
[
{
"_id": "女",
"value": 5056,
"avg_followers": 1736109.3710443038,
"avg_weibo_num": 5647.630735759493,
"avg_following": 450.856210443038,
"max_followers": 182775632,
"max_weibo_num": 374663,
"max_following": 20000,
"name": "女"
},
{
"_id": "男",
"value": 5444,
"avg_followers": 2158212.2224467304,
"avg_weibo_num": 7545.73989713446,
"avg_following": 569.4851212343865,
"max_followers": 110417438,
"max_weibo_num": 756859,
"max_following": 20000,
"name": "男"
}
]路由:GET /api/statistic/location
返回数据:
[
{
"name": "安徽",
"value": 93
}
]数据库采用MongoDB
数据库中共有4个Collection:blogger_info、weibo_content、blogger_friend、user_id,以下进行详细说明
- 含义:存储博主的个人信息
- 字段说明
- id:博主的微博ID
- name:微博昵称
- sex:性别
- birthplace:生源地
- weibo_num:微博数
- following:关注好友数
- followers:粉丝数
- 含义:存储博主的微博详情
- 字段说明
- id:博主的微博ID
- weibo_content:微博详情
- publish_content:发布内容
- publish_location:发布位置
- publish_time:发布时间
- like:点赞数
- forward:转发数
- comment:评论数
- 含义:存储博主的好友列表
- 字段说明
- id:博主的微博ID
- friends:好友列表
- id:好友的微博ID
- 含义:存储全部博主的微博ID
- 字段说明
- id:博主的微博ID
- 特殊说明:该collection是一边爬取博主的好友,一边存储其好友ID
在命令行界面分别输入以下命令:
mongoimport -d Weibo -c blogger_info (weibo-mongodb-data 的路径)/blogger_info.dat
mongoimport -d Weibo -c weibo_content (weibo-mongodb-data 的路径)/weibo_content.dat
mongoimport -d Weibo -c blogger_friend (weibo-mongodb-data 的路径)/blogger_friend.dat
mongoimport -d Weibo -c user_id (weibo-mongodb-data 的路径)/user_id.dat
weibo-mongodb-data 存储了数据库中所有collection的数据,数据库包含 10500 位微博用户的基本信息、好友信息、微博信息,大约600M 数据,需要的话,联系作者 liangjiakun@zju.edu.cn
- 博主的好友列表:即博主的关注列表
- 对于关注列表、粉丝列表,新浪微博最多只能查看最新关注的200个用户,跟爬虫技术无关
- 为了便于数据分析,在爬取过程中进行了如下剪枝(在config.py中可以自定义):
- 每位博主最多只爬取20位好友
- 每位博主最多只爬取1年以内的微博信息,同时限制不超过300条
- 好友列表中只有好友的id字段信息,如果需要获取好友的基本信息和微博信息等,可以用id到基本信息表和微博信息表中查找















