鱼C论坛

 找回密码
 立即注册
查看: 315|回复: 4

python爬虫爬取b站视频评论问题在哪里?出错找不到,大佬看看代码求求大佬帮忙....

[复制链接]
发表于 2025-3-7 21:43:48 | 显示全部楼层 |阅读模式

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

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

x
import requests
import csv
import hashlib
import time

from urllib.parse import quote
#w_rid加密参数

def GetW(wts,NextPage):
    pagination_str = quote(NextPage)
    l = ["mode=2",
         "oid=113814703972019",
         f"pagination_str={pagination_str}",
         "plat=1",
         "type=1",
         "web_location=1315875",
         f"wts={wts}"
         ]
    y = '&'.join(l)
    string  = y + "ea1db124af3c7062474693fa704f4ff8"
    MD5 = hashlib.md5()
    MD5.update(string.encode('utf-8'))
    w_rid = MD5.hexdigest()
    print(w_rid)
    return w_rid











def GetContent(offset):
  headers = {"cookie":"bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NDE1Nzk0MzAsImlhdCI6MTc0MTMyMDE3MCwicGx0IjotMX0.FsFshnZPFdht_Fj7Pxqp5XgzHlgnYPnpoGWX53F1imw; bili_ticket_expires=1741579370; buvid3=1D55509D-6660-9458-1C1A-6EC43569BE4130240infoc; b_nut=1741320227; buvid4=6A35DDCB-75FC-4C6D-03B6-B828DEA70BA430240-025030704-QwNGI+jNopUTI1GhLVomaw%3D%3D; _uuid=1D11D2210-2BC9-2529-8C44-C1010A76E8D381028745infoc; CURRENT_FNVAL=4048; buvid_fp=2f0eefefa84c6a4ab0d42472a63a6dd4; b_lsid=D414ECEB_1956F97B63D; csrf_state=130ae0096803953f25dfdd2b87144e35; SESSDATA=cdefeabd%2C1756887034%2C4ca20%2A32CjD7yyeY9-VFI_RWy8IsjId3cPUs-xTs-3CYvAyVNuAehtfvU-_ii9uXRESeWr7O9I4SVlVNQUFBUzR6NkxLcDh6SGhvVWtPbWVqZUJ6U2t6OFgtYVdqVEFJb3ZtSjJmTUQ3VHBlRFh5LXFNSDN0bUlOTlgwMF93RVV4aHh5TXB5N3o0SVRDOUdRIIEC; bili_jct=1018cac8e70a199a4feca14132a9055c; DedeUserID=3546377353169818; DedeUserID__ckMd5=b43dfea08718ba45; sid=mcmifm2h",               "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0",}

,}


    url = 'https://api.bilibili.com/x/v2/reply/wbi/main'
    pagination_str =   '{"offset":%s}' % offset
    wts = int(time.time())
    w_rid = GetW(wts = wts,NextPage = pagination_str)
    data = {
        'oid': '114040961501285',
        'type': '1',
        'mode':' 2',
        'pagination_str': '{"offset":%s}' % offset,
        'plat': '1',
        'seek_rpid': '',
        'web_location': '1315875',
        'w_rid': w_rid,
        'wts': 'wts',}
    response = requests.get(url = url, params = data ,headers = headers)
    #Get查询参数
    #请求方法:Get请求方法

    #  获取数据
    json_data = response.json()

    #  解析数据
    #  字典取值
    replies = json_data['data']['replies']
    for index in replies:
        dit = {
            '昵称':index['member']['uname'],
            '性别' :index['member']['sex'],
            '地区':index['reply_control']['location'].replace('IP属地',''),
            '评论':index['content']['message'],
            '点赞':index['like'],}
        print(dit)
        csv_writer.writerow(dit)
#  保存数据
#  批量采集数据
#  paginaion_str    第一页为空,后续一样
#  wts:时间戳  可以通过time模块获取当前时间戳
#  w_rid:加密参数·
    NextPage = json_data['data']['cursor']['pagination_reply']['next_offset']
    next_offset = json.dumps(NextPage)
    print
    return next_offset
if __name__ == '__main__':
    #  发送请求
    #cookie 用户信息,常用于检测是否有登录账号
    #user-agent 用户代理,表述浏览器用户身份信息
    f = open(file = 'data.csv',mode = 'w',encoding = 'utf-8',newline = '')
    #字典写入方法
    csv_writer = csv.DictWriter(f,fieldnames = ['昵称','性别','地区','评论','点赞'])
    #
    csv_writer.writeheader()
    offset = '""'
    for page in range(1,21):
            offset =  GetContent(offset = offset)
企业微信截图_17413522725308(1).png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2025-3-7 21:44:22 | 显示全部楼层
让我帮您看看这段代码的问题。您提供的代码存在一些明显的语法错误与逻辑错误。请尝试以下修正:

1. `l =`(行不完整) - 在 GetW 函数中的这个行需要定义 `l`。
   
2. DitWriter 使用时缺少字段名列表。

3. requests.get 应该使用在 wts 参数,而不是 'wts'

4. 返回的 JSON 数据中 replies 的结构需要检查,因为对其的处理似乎有误。

改正后的代码如下:


  1. import requests
  2. import csv
  3. import hashlib
  4. import time
  5. from urllib.parse import quote

  6. def GetW(wts, NextPage):
  7.     pagination_str = quote(NextPage)
  8.     l = [
  9.         f"pagination_str={pagination_str}",
  10.         f"wts={wts}"
  11.     ]
  12.     y = '&'.join(l)
  13.     string = y + "ea1db124af3c7062474693fa704f4ff8"
  14.     MD5 = hashlib.md5()
  15.     MD5.update(string.encode('utf-8'))
  16.     w_rid = MD5.hexdigest()
  17.     print(w_rid)
  18.     return w_rid

  19. def GetContent(offset):
  20.     headers = {
  21.         "cookie": "your_cookie_here",
  22.         "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0"
  23.     }

  24.     url = '[url=https://api.bilibili.com/x/v2/reply/wbi/main']https://api.bilibili.com/x/v2/reply/wbi/main'[/url]
  25.     pagination_str = '{"offset":%s}' % offset
  26.     wts = int(time.time())
  27.     w_rid = GetW(wts=wts, NextPage=pagination_str)
  28.    
  29.     data = {
  30.         'oid': '114040961501285',
  31.         'type': '1',
  32.         'mode': '2',
  33.         'pagination_str': pagination_str,
  34.         'plat': '1',
  35.         'seek_rpid': '',
  36.         'web_location': '1315875',
  37.         'w_rid': w_rid,
  38.         'wts': wts,
  39.     }
  40.    
  41.     response = requests.get(url=url, params=data, headers=headers)
  42.    
  43.     json_data = response.json()

  44.     replies = json_data.get('data', {}).get('replies', [])

  45.     for index in replies:
  46.         dit = {
  47.             '昵称': index.get('member', {}).get('uname', ''),
  48.             '性别': index.get('member', {}).get('sex', ''),
  49.             '地区': index.get('member', {}).get('region', '').replace('IP属地', ''),
  50.             '评论': index.get('content', {}).get('message', ''),
  51.             '点赞': index.get('like', 0),
  52.         }
  53.         print(dit)
  54.         csv_writer.writerow(dit)

  55.     next_offset = json_data.get('data', {}).get('page', {}).get('next', "")
  56.     print(next_offset)
  57.     return next_offset

  58. if __name__ == '__main__':
  59.     f = open(file='data.csv', mode='w', encoding='utf-8', newline='')
  60.    
  61.     fieldnames = ['昵称', '性别', '地区', '评论', '点赞']
  62.     csv_writer = csv.DictWriter(f, fieldnames=fieldnames)
  63.    
  64.     csv_writer.writeheader()
  65.     offset = ""
  66.    
  67.     for page in range(1, 21):
  68.         offset = GetContent(offset=offset)

  69.     f.close()
复制代码


请确保用您自己的 cookies 替换 "your_cookie_here"。希望这可以帮助您找到并解决问题。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-3-7 23:47:37 | 显示全部楼层

回帖奖励 +3 鱼币

确保您的网络环境可以访问Bilibili API
请检查所提供的cookie是否有效且符合Bilibili的登录状态要求,因为某些API接口可能需要用户登录才能访问
考虑到API速率限制或反爬虫机制,频繁请求可能导致IP被暂时封禁或其他限制,请合理设置请求间隔
对于data.csv文件路径,确保脚本有权限在该路径下创建和写入文件
在实际运行此脚本前,建议先测试单次请求是否能正常工作,以确保所有参数配置正确
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-3-8 00:11:23 | 显示全部楼层
被反爬了怎么办呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2025-3-10 14:54:04 | 显示全部楼层
- 修复了wts参数错误,使用实际时间戳替代字符串'wts'
- 优化了pagination_str参数结构,使用json序列化方式
- 重构了加密参数生成逻辑,确保符合B站API要求
- 添加了异常处理机制和API响应状态检查
- 规范了代码格式,移除了冗余代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-5 19:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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