目录
3.3 数据分析 - 视频时长与用户互动(点赞、评论)关系分析
一、案例背景
短视频内容创作行业在移动互联网普及的浪潮中迅速崛起,成为互联网领域极具活力的板块。它以简洁、生动、碎片化的内容形式,满足了用户多样化的娱乐、学习和社交需求。从搞笑娱乐、生活记录到知识科普、创意广告,短视频涵盖了广泛的题材。然而,行业发展面临着内容同质化严重、优质内容创作难度大、流量竞争激烈以及版权问题等挑战。借助 Python 对短视频内容创作行业相关数据进行分析,能够助力创作者洞察用户喜好、优化内容策略,也能帮助平台提升内容推荐精准度、增强用户粘性。
二、代码实现
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import requests
from bs4 import BeautifulSoup
2.1 数据收集
数据来源主要包括行业研究机构报告(如 QuestMobile、艾瑞咨询)、短视频平台数据(如抖音、快手开放平台提供的部分数据)、社交媒体上关于短视频的讨论以及搜索引擎相关搜索数据。
- 从 QuestMobile 网站抓取短视频行业用户规模数据:
url = 'https://www.questmobile.com.cn/research/report - detail?reportId=202401091631156381'
headers = {
'User - Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url, headers = headers)
soup = BeautifulSoup(response.text, 'html.parser')
user_scale_data = []
table = soup.find('table', class_='report - table')
rows = table.find_all('tr')
for row in rows[1:]:
cols = row.find_all('td')
month = cols[0].text.strip()
user_scale = int(cols[1].text.strip().replace(',', ''))
user_scale_data.append({'Month': month, 'User_Scale': user_scale})
user_scale_df = pd.DataFrame(user_scale_data)
- 从某短视频平台 API 获取视频播放量、点赞数及评论数数据(假设已获得平台授权):
import json
api_url = 'https://api.shortvideoplatform.com/video_metrics'
headers = {
'Authorization': 'your_api_key',
'Content - Type': 'application/json'
}
response = requests.get(api_url, headers = headers)
if response.status_code == 200:
video_metrics_data = json.loads(response.text)
video_metrics_df = pd.DataFrame(video_metrics_data)
else:
print('Failed to get video metrics data')
2.2 数据探索性分析
# 查看用户规模数据基本信息
print(user_scale_df.info())
# 查看视频播放量、点赞数及评论数数据基本信息
print(video_metrics_df.info())
# 分析短视频行业用户规模随时间变化趋势
user_scale_df['Month'] = pd.to_datetime(user_scale_df['Month'])
plt.figure(figsize=(12, 6))
sns.lineplot(x='Month', y='User_Scale', data=user_scale_df)
plt.title('Trend of Short - Video Industry User Scale')
plt.xlabel('Month')
plt.ylabel('User Scale (person)')
plt.show()
# 查看不同视频类型的平均播放量
video_type_play = video_metrics_df.groupby('Video_Type')['Play_Count'].mean()
plt.figure(figsize=(10, 6))
sns.barplot(x=video_type_play.index, y=video_type_play.values)
plt.title('Average Play Count of Different Video Types')
plt.xlabel('Video Type')
plt.ylabel('Average Play Count')
plt.xticks(rotation=45)
plt.show()
2.3 数据清洗
# 用户规模数据清洗
# 检查并处理缺失值
user_scale_df.dropna(inplace = True)
# 去除重复记录
user_scale_df = user_scale_df.drop_duplicates()
# 视频播放量、点赞数及评论数数据清洗
# 处理异常播放量数据,如播放量为负数等情况
video_metrics_df = video_metrics_df[video_metrics_df['Play_Count'] > 0]
# 处理异常点赞数和评论数数据,如点赞数或评论数为负数
video_metrics_df = video_metrics_df[(video_metrics_df['Like_Count'] > 0) & (video_metrics_df['Comment_Count'] > 0)]