鱼C论坛

 找回密码
 立即注册
查看: 3102|回复: 0

爬取B站评论,下面代码只能爬取一级评论,有没有大佬指导以下如何爬取交互回复信息.

[复制链接]
发表于 2023-11-10 20:05:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import requests
  2. import re
  3. import time
  4. import csv

  5. # 消息头信息
  6. header = {
  7.     'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  8.     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
  9. }

  10. # 获取评论API
  11. original_url = 'https://api.bilibili.com/x/v2/reply/main?jsonp=jsonp&next={}&type=1&oid={}&mode=3'

  12. # 时间戳转换成日期
  13. def get_time(ctime):
  14.     timeArray = time.localtime(ctime)
  15.     otherStyleTime = time.strftime("%Y.%m.%d", timeArray)
  16.     return str(otherStyleTime)

  17. # 获取aid
  18. def get_oid(bvid):
  19.     video_url = 'https://www.bilibili.com/video/' + bvid
  20.     page = requests.get(video_url, headers=header).text
  21.     aid = re.search(r'"aid":[0-9]+', page).group()[6:]
  22.     return aid

  23. # 边爬取评论边保存文件
  24. def online_save(bvid):
  25.     all_count = 0
  26.     oid = get_oid(bvid)
  27.     page = 1
  28.     url = original_url.format(page, oid)
  29.     html = requests.get(url, headers=header)
  30.     data = html.json()
  31.     count = int(data['data']['cursor']['all_count'])
  32.     fname = bvid + '_评论.csv'
  33.     with open(fname, 'w+', newline='', encoding='utf_8_sig') as f:
  34.         csv_writer = csv.writer(f)
  35.         csv_writer.writerow(["时间", "点赞", "用户名", "评论"])  # Added "用户名" header
  36.         for i in data['data']['replies']:
  37.             message = i['content']['message']
  38.             message = re.sub('\s+', '', message)
  39.             ctime = get_time(i['ctime'])
  40.             like = i['like']
  41.             username = i['member']['uname']  # Added to get username
  42.             csv_writer.writerow([ctime, str(like), username, message])  # Added username
  43.             all_count = all_count + 1
  44.             
  45.             # Check for and collect reply comments
  46.             if 'replies' in i:
  47.                 for reply in i['replies']:
  48.                     reply_message = reply['content']['message']
  49.                     reply_message = re.sub('\s+', '', reply_message)
  50.                     reply_like = reply['like']
  51.                     reply_username = reply['member']['uname']
  52.                     csv_writer.writerow(["REPLY", str(reply_like), reply_username, reply_message])

  53.         print('总评论数:{},当前评论数:{},爬取Page{}完毕。'.format(count, all_count, page))
  54.         time.sleep(5)
  55.         while all_count < count:
  56.             page += 1
  57.             url = original_url.format(page, oid)
  58.             try:
  59.                 html = requests.get(url, headers=header)
  60.                 data = html.json()
  61.                 for i in data['data']['replies']:
  62.                     message = i['content']['message']
  63.                     ctime = get_time(i['ctime'])
  64.                     like = i['like']
  65.                     username = i['member']['uname']
  66.                     csv_writer.writerow([ctime, str(like), username, message])
  67.                     all_count = all_count + 1
  68.                     
  69.                     # Check for and collect reply comments
  70.                     if 'replies' in i:
  71.                         for reply in i['replies']:
  72.                             reply_message = reply['content']['message']
  73.                             reply_message = re.sub('\s+', '', reply_message)
  74.                             reply_like = reply['like']
  75.                             reply_username = reply['member']['uname']
  76.                             csv_writer.writerow(["REPLY", str(reply_like), reply_username, reply_message])

  77.                 print('总评论数:{},当前评论数:{},爬取Page{}完毕。'.format(count, all_count, page))
  78.                 time.sleep(5)
  79.             except:
  80.                 break
  81.         f.close()

  82. if __name__ == '__main__':
  83.     bvid = input('输入视频Bvid:')
  84.     online_save(bvid)
  85.     print('完成!')
复制代码
@不二如是
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 12:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表