Python 爬取 20 万条评论,告诉你周杰伦新歌为啥弄崩 QQ 音乐?
以下文章来源于不正经程序员 ,作者哈哈浩
作者 | 哈哈浩
责编 | 伍杏玲
数据获取
1、请求分析
url = "https://c.y.qq.com/base/fcgi-bin/fcg_global_comment_h5.fcg"
querystring = {"biztype":"1","topid":"237773700","cmd":"8","pagenum":"1","pagesize":"25"}
response = requests.request("GET", url, params=querystring)
print(response.text)
2、数据解析
{
"nick": "丨那壹刻永遠消失\"\"",
"praisenum": 1,
"rootcommentcontent": "越听越好听怎么回事!",
"time": 1568729836,
}
由于数据量较大 这里我们暂时将数据存放在 Excel 中,一来无须依赖外部数据库,二来可以使用 Excel 对数据进行二次处理。
数据存储代码如下:
def file_do(list_info, file_name):
# 获取文件大小
if not os.path.exists(file_name):
wb = openpyxl.Workbook()
page = wb.active
page.title = 'jay'
page.append(['昵称','时间','点赞数','评论'])
else:
wb = openpyxl.load_workbook(file_name)
page = wb.active
for info in list_info:
try:
page.append(info)
except Exception:
print(info)
wb.save(filename=file_name)
数据可视化
1、各时段的评论数
2、大家都在说什么
3、大家都点赞了哪些评论
【END】
热 文 推 荐