求解答!
使用protobuf进行反序列化时持续报错,但运行的是给出示例代码啊!
GitHub项目为bilibili-API-collect
项目地址为:https://github.com/SocialSisterYi/bilibili-API-collect
使用的protobuf文件是项目仓库中的grpc_api/bilibili/community/service/dm/v1/dm.proto
项目readme中protobuf弹幕部分的示例代码为:
import requests
import google.protobuf.text_format as text_format
import bilibili.community.service.dm.v1.dm_pb2 as Danmaku
url = 'https://api.bilibili.com/x/v2/dm/web/seg.so'
params = {
'type': 1, # 弹幕类型
'oid': 1176840, # cid
'pid': 810872, # avid
'segment_index': 1 # 弹幕分段
}
resp = requests.get(url, params)
data = resp.content
danmaku_seg = Danmaku.DmSegMobileReply()
danmaku_seg.ParseFromString(data)
print(text_format.MessageToString(danmaku_seg.elems[0], as_utf8=True))
本人的操作步骤为:在GitHub中下载最新的protobuf编译器,版本为v.6.30.2(Github项目地址为:https://github.com/protocolbuffers/protobuf)。
利用cmd执行生成python可执行文件后,得到后缀为_pb2.py文件后,放入同级目录中运行示例代码。
结果运行时报错为:
Traceback (most recent call last):
google.protobuf.message.DecodeError: Error parsing message with type 'dm.DmSegMobileReply'