Skip to content

Commit

Permalink
fix: comment.get_comments_lazy
Browse files Browse the repository at this point in the history
评论跑去需要更新了 #871
  • Loading branch information
Nemo2011 committed Dec 20, 2024
1 parent e97e8e0 commit ffd8123
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
5 changes: 3 additions & 2 deletions bilibili_api/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ async def get_comments_lazy(
type_ (CommentsResourceType) : 资源类枚举。
offset (str, optional) : 偏移量。每次请求可获取下次请求对应的偏移量,类似单向链表。
offset (str, optional) : 偏移量。每次请求可获取下次请求对应的偏移量,类似单向链表。对应返回结果的 `["cursor"]["pagination_reply"]["next_offset"]`
order (OrderType, optional) : 排序方式枚举. Defaults to OrderType.TIME.
Expand All @@ -461,11 +461,12 @@ async def get_comments_lazy(
"""
offset = offset.replace('"', '\\"')
offset = '{"offset":"' + offset + '"}'
old_to_new = {0: 2, 2: 3}
api = API["comment"]["reply_by_session_id"]
params = {
"oid": oid,
"type": type_.value,
"mode": order.value,
"mode": old_to_new[order.value],
"pagination_str": offset,
"web_location": "1315875",
}
Expand Down
48 changes: 48 additions & 0 deletions docs/examples/comment.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 示例:获取视频所有评论

**建议登录后操作**

## 旧接口

```python
from bilibili_api import comment, sync

Expand Down Expand Up @@ -43,3 +47,47 @@ async def main():
sync(main())
```

## 新接口

``` python
from bilibili_api import comment, sync


async def main():
# 存储评论
comments = []
# 刷新次数(约等于页码)
page = 1
# 每次提供的 offset (pagination_str)
pag = ""

while True:
# 获取评论
c = await comment.get_comments_lazy(418788911, comment.CommentResourceType.VIDEO, offset=pag)

pag = c["cursor"]["pagination_reply"]["next_offset"]
replies = c['replies']
if replies is None:
# 未登陆时只能获取到前20条评论
# 此时增加页码会导致c为空字典
break

# 存储评论
comments.extend(replies)
# 增加页码
page += 1

# 只刷新 5 次(约等于 5 页)
if page > 5:
break

# 打印评论
for cmt in comments:
print(f"{cmt["member"]['uname']}: {cmt['content']['message']}")

# 打印评论总数
print(f"\n\n共有 {len(comments)} 条评论(不含子评论)")


sync(main())
```

0 comments on commit ffd8123

Please sign in to comment.