B站评论采集

B站评论采集

打开目标网址

哔哩哔哩 (゜-゜)つロ 干杯~-bilibili

image.png

找到爬取得剧的评论,打开浏览器抓包工具进行抓包分析:

image.png

这里爬取鬼灭之刃第一季的评论数据,
分析网页

打开评论页面,可以看到分为短评(128702)和长评(639)条。常规操作直接F12打开网络观察发送的请求数据。

在评论页面往下翻几下就可以看到有个url是“list?“开头的,点开预览果然就是哔哩哔哩评论的api了。找到了api就很好办了,直接分析一下api的组成和需要传递的参数。

分析b站api
 复制代码 隐藏代码
https://api.bilibili.com/pgc/review/short/list?media_id=22718131&ps=20&sort=0&cursor=83215035767195

观察一下链接,有个short字段,这个就是对应的短评,如果换成long就是长评。

”ps=20“就是一页的数据。”media_id=22718131“就是番剧的id。

”cursor“字段就是数据的地址,每次请求数据返回的json数据都会有下一页的cursor。

image.png

下面就是用requests进行请求写代码

先导入库:

import requests
import json
import time
import csv

爬取代码:

def levels_get(mid, proxies):

    url = f'https://api.bilibili.com/x/space/wbi/acc/info?mid={str(mid)}'
    header = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
        'cookie': "_uuid=B31794AC-389B-5ABE-431A-4A457532710F563197infoc; buvid3=90E810B6-24C7-0FD9-FCEB-FFAAD82CE42266124infoc; b_nut=1666688064; buvid4=C5894024-2549-4CC5-B6CC-4CE4F3C6025F66124-022102516-ueloTil9hhszmIQpPi1hPg==; buvid_fp_plain=undefined; i-wanna-go-back=-1; nostalgia_conf=-1; rpdid=|(u))umkR)l|0J'uYY)Y~lkkl; bsource=search_google; LIVE_BUVID=AUTO2616698028069789; is-2022-channel=1; CURRENT_BLACKGAP=0; CURRENT_FNVAL=4048; CURRENT_QUALITY=120; fingerprint=5fdc996e8cc80fd6b9c56ffc54ce7181; sid=8dtevv76; bp_video_offset_83512806=744605732851155000; buvid_fp=5fdc996e8cc80fd6b9c56ffc54ce7181; b_lsid=F72104910B_18557CE9216; b_ut=7; innersign=1; PVID=8",
    }
    try:
        rs = requests.get(url, headers=header,proxies=proxies)
    except:
        proxies = gengxin()

        rs = requests.get(url, headers=header, proxies=proxies, timeout=10)
    datad = json.loads(rs.text)
    try:
        return datad['data']['level']
    except:
        return 'Nah'

cursor = 78670930957092
while cursor != '老6':
    url = f'https://api.bilibili.com/pgc/review/long/list?media_id=22718131&ps=20&sort=0&cursor={cursor}'
    print(url)
    while True:
        try:
            r = requests.get(url, timeout=5)
            data = json.loads(r.text)
            break
        except:
            print('请求超时')
            time.sleep(1)

    for i in data['data']['list']:
        uname = i['author']['uname']#用户昵称
        userid = i['author']['mid']#用户id
        score = i['score']#用户评分
        content = i['content']#用户评论内容
        time_unix = int(i['mtime'])
        times = timefun(time_unix)
        # middd = levels_get(userid, proxies)
        middd = 'Nah'
        print(times, uname, userid, middd, score, content)
        with open("test.csv", "a", newline='',encoding='UTF8') as csvfile:
            writer = csv.writer(csvfile)
            # 写入多行用writerows
            writer.writerow([times, uname, userid, middd, score, content])

    cursor = data['data']['next']
    print(f'下一个页面{cursor}')

完整代码:

import requests
import json
import time
import csv

'''包括评分用户id及会员等级、具体评分星级、长评以及短评的评论内容、评分时间,'''

def timefun(time1):
    time1 = int(time1)
    timeArray = time.localtime(time1)
    times = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)  # 时间
    return times

def levels_get(mid, proxies):

    url = f'https://api.bilibili.com/x/space/wbi/acc/info?mid={str(mid)}'
    header = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
        'cookie': "_uuid=B31794AC-389B-5ABE-431A-4A457532710F563197infoc; buvid3=90E810B6-24C7-0FD9-FCEB-FFAAD82CE42266124infoc; b_nut=1666688064; buvid4=C5894024-2549-4CC5-B6CC-4CE4F3C6025F66124-022102516-ueloTil9hhszmIQpPi1hPg==; buvid_fp_plain=undefined; i-wanna-go-back=-1; nostalgia_conf=-1; rpdid=|(u))umkR)l|0J'uYY)Y~lkkl; bsource=search_google; LIVE_BUVID=AUTO2616698028069789; is-2022-channel=1; CURRENT_BLACKGAP=0; CURRENT_FNVAL=4048; CURRENT_QUALITY=120; fingerprint=5fdc996e8cc80fd6b9c56ffc54ce7181; sid=8dtevv76; bp_video_offset_83512806=744605732851155000; buvid_fp=5fdc996e8cc80fd6b9c56ffc54ce7181; b_lsid=F72104910B_18557CE9216; b_ut=7; innersign=1; PVID=8",
    }
    try:
        rs = requests.get(url, headers=header,proxies=proxies)
    except:
        proxies = gengxin()

        rs = requests.get(url, headers=header, proxies=proxies, timeout=10)
    datad = json.loads(rs.text)
    try:
        return datad['data']['level']
    except:
        return 'Nah'

cursor = 78670930957092
while cursor != '老6':
    url = f'https://api.bilibili.com/pgc/review/long/list?media_id=22718131&ps=20&sort=0&cursor={cursor}'
    print(url)
    while True:
        try:
            r = requests.get(url, timeout=5)
            data = json.loads(r.text)
            break
        except:
            print('请求超时')
            time.sleep(1)

    for i in data['data']['list']:
        uname = i['author']['uname']#用户昵称
        userid = i['author']['mid']#用户id
        score = i['score']#用户评分
        content = i['content']#用户评论内容
        time_unix = int(i['mtime'])
        times = timefun(time_unix)
        # middd = levels_get(userid, proxies)
        middd = 'Nah'
        print(times, uname, userid, middd, score, content)
        with open("test.csv", "a", newline='',encoding='UTF8') as csvfile:
            writer = csv.writer(csvfile)
            # 写入多行用writerows
            writer.writerow([times, uname, userid, middd, score, content])

    cursor = data['data']['next']
    print(f'下一个页面{cursor}')

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
****************************模板规范化管理 开始**************************** 每个模版要把所用的css,js,img,html按照相关规定,放到指定目录。 每个模版都可定义不同的样式,所以系统内置的功能的相关文件也都放在了模版里,每个模版里存储一份,避免替换系统目录下的其他文件。 ads/ 存放广告js文件,可自定义名称, 在当前模版路径的config.xml 里配置好 html/ 存放html模版文件,可自定义名称,在后台配置好。 js/ 存放模版相关js文件 js/home.js 系统内置功能的JS函数库 css/ 存放样式表文件 css/home.css 系统内置功能的样式表,例如 顶踩,联想搜索,星星评分,历史记录,留言本,评论等功能。 css/style.css 当前模版的自定义样式表 images/ 存放模版的相关图片 images/home/ 存放系统内置功能的相关图片,例如 顶踩,联想搜索,星星评分,历史记录,留言本,评论等功能。 images/ads/ 存放广告图片 template/user/ 为系统会员中心的模版及相关css和js ****************************模板规范化管理 结束**************************** ****************************系统内置JS、CSS说明 开始**************************** 文件:js/home.js MAC.Url 当前网页的链接 MAC.Title 当前网页的标题 MAC.Copy(s) 复制内容到剪切板; s=字符串 MAC.Home(o,u) 设置默认主页; 设置主页 MAC.Fav(u,s) 加入浏览器收藏夹; 加入收藏 MAC.Open(u,w,h) 弹出网页;u=网址,w=宽度,h=高度 MAC.Cookie.Set(name,value,days) 设置cookie的值; name=cookie名称,value=cookie值,days=过期时间 MAC.Cookie.Get(name) 获取cookie的值; name=cookie名称 MAC.Cookie.Del(name) 删除cookie的值; name=cookie名称 MAC.AdsWrap(w,h,n) 预留广告位占位; w=宽度,h=高度,n=名称 自动加载设置项: html元素ID为history: 自动设置为鼠标移动滑入滑出 显示隐藏 历史记录。 html元素ID为wd: 自动设置联想搜索功能。 延迟加载图片: img元素不要使用src调用图片地址,而是用data-original。 ****************************系统内置JS、CSS函数库说明 结束**************************** ****************************模板规范化管理 结束**************************** ****************************模板介绍开始**************************** home_include.html 全站公共引入文件 引入js、css样式,还有系统JS变量 home_head.html 全站头部 home_foot.html 全站尾部 home_gbook.html 留言本 home_comment.html 评论 label_ 开头的都是自定义页面 art_detail.html 文章内容页 art_index.html 文章首页 art_list.html 文章分类筛选页 art_map.html 文章地图页 art_search.html 文章搜索页 art_topicindex.html 文章专题首页 art_topiclist.html 文章专题数据列表页 art_type.html 文章分类页 vod_detail.html 视频内容页 vod_index.html 视频首页 vod_list.html 视频分类筛选页 vod_map.html 视频地图页 vod_play.html 视频播放页 vod_playopen.html 视频弹窗播放页 vod_search.html 视频搜索页面 vod_topicindex.html 视频专题首页 vod_topiclist.html 视频专题数据列表页 vod_type.html 视频分类页面 ****************************模板介绍结束**************************** ****************************全局标签开始**************************** {maccms:runtime} 页面运行时间、查询次数、占用内存 {maccms:date} 当前日期 {maccms:siteaid} 当前所在模块ID {maccms:url} 网站域名 {maccms:name} 网站名称 {maccms:keywords} 网站关键字 {maccms:description} 网站描述信息 {maccms:icp} 网站备案号 {maccms:qq} 网站管理QQ {maccms:email} 网站管理Email {maccms:visits} 网站统计代码 {maccms:path} 安装目录 {maccms:path_ads} 当前模版广告文件目录 {maccms:path_tpl} 当前模版HTML文件目录 {maccms:suffix} 文件后缀名 {maccms:link_gbook} 留言本链接 {maccms:link_search_vod} 视频搜索页链接 {maccms:link_search_art} 文章搜索页链接 {maccms:link_index} 视频首页链接 {maccms:link_index_art} 文章首页链接 {maccms:link_map_vod} 视频地图链接 {maccms:link_map_art} 文章地图链接 {maccms:link_topic_vod} 视频专题首页链接 {maccms:link_topic_art} 文章专题首页链接 {maccms:link_map_rss} RSS链接 {maccms:link_map_baidu} Baidu SiteMap链接 {maccms:link_map_google} Google SiteMap链接 {maccms:count_vod_all} 视频数据总量 {maccms:count_vod_day} 视频当天更新数据量 {maccms:count_art_all} 文章数据总量 {maccms:count_art_day} 文章当天更新数据量 {maccms:count_user_all} 会员总数 {maccms:count_user_day} 会员当天注册数量 {maccms:userid} 当前登录会员ID {maccms:username} 当前登录会员名 {maccms:usergroupid} 当前登录会员组ID,如果没有登录则是空 {maccms:curvodtypeid} 视频当前分类ID {maccms:curvodtypepid} 视频当前分类的父分类ID {maccms:curvodtopicid} 视频当前专题ID {maccms:curarttypeid} 文章当前分类ID {maccms:curarttypepid} 文章当前分类的父分类ID {maccms:curarttopicid} 文章当前专题ID {maccms:load label.html} 载入自定义页面内容 {maccms:getlink label.html} 获取自定义页面的链接 {maccms:siteaid} 当前所在系统模版id 视频首页 10 视频地图页 11 视频分类,筛选页 12 视频专题首页 13 视频专题数据列表 14 视频搜索页 15 视频内容页 16 视频播放页 17 视频下载页 18 文章首页 20 文章地图页 21 文章分类,筛选页 22 文章专题首页 23 文章专题数据列表 24 文章搜索页 25 文章内容页 26 系统留言本 30 系统评论 31 系统用户中心 40 ****************************全局标签结束**************************** ****************************条件判断if标签 开始**************************** if标签,支持多重嵌套,每个层级的if标签不能相同 例: {if-A:[vod:num] > 1 }....{endif-A} {if-A:[vod:num] mod 2=0}....{else-A}....{endif-A} {if-A:[vod:num] mod 2=0}....{elseif-A}....{else-A}....{endif-A} {if-A:not isN("[vod:remarks]")} [vod:remarks] {elseif-A:[vod:state]=0} [完结] {elseif-A:[vod:state]>0} {if-B:[vod:state]>10000} [连载[vod:state]]期 {else-B} [连载[vod:state]]集 {endif-B} {else-A} {endif-A} ****************************条件判断if标签 结束**************************** ****************************用户登陆页面标签 开始**************************** 用户登录窗口iframe调用: <iframe src="{

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Deng872347348

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值