starry吧 关注:375贴子:2,627
  • 6回复贴,共1
import asyncio
import time
from typing import List
import aiotieba as tb
from aiotieba.logging import get_logger as LOG
BDUSS = ""
async def main():
async with tb.Client(BDUSS) as client:
# [1] 什么是`asyncio.gather`?
# 参考官方文档:并发运行任务
# https://docs.python.org/zh-cn/3/library/asyncio-task.html#running-tasks-concurrently
user, threads = await asyncio.gather(client.get_self_info(), client.get_threads('电脑', pn=200, rn=100))
search_txt = await asyncio.gather(client.search_exact('电脑', '配置', pn = 100, rn = 30, only_thread = False))
print(search_txt)
all_huifu_list = []
for thread in threads:
huifu_list = []
post = await asyncio.gather(client.get_posts(
thread.tid, pn=50, rn=30, only_thread_author=False,
with_comments=True, comment_sort_by_agree=True, comment_rn=50)
)
for i in range(len(post[0])):
huifu = post[0][i]
huifu_list.append(huifu)
all_huifu_list += huifu_list
# 将获取的信息打印到日志
print(f"当前用户: {user}")
# for thread in threads:
# # Threads支持迭代,因此可以使用for循环逐条打印主题帖信息
# # 当然了,Threads也支持使用下标的随机访问
# print(f"tid: {thread.tid} 最后回复时间戳: {thread.last_time} 标题: {thread.title}")
print(all_huifu_list)
with open("data.txt", "a") as f:
for huifu in all_huifu_list:
f.write(str(huifu))
f.write('\n')
# 使用asyncio.run执行协程main
asyncio.run(main())
1. 使用 search_txt = await asyncio.gather(client.search_exact('电脑', '配置', pn = 100, rn = 30, only_thread = False))我发现没结果
2. post = await asyncio.gather(client.get_posts(
thread.tid, pn=50, rn=30, only_thread_author=False,
with_comments=True, comment_sort_by_agree=True, comment_rn=50)
)
我该如何直接从post里拿到每层楼的text以及他对应的回复,这样我不就不需要用正则提取出来了
谢谢


IP属地:浙江1楼2024-04-11 21:57回复
    1.我这能搜到结果。另外gather里面只有一个对象的话就没必要用gather了
    2.for post in posts: for comment in post.comments: print(comment.text)就能拿到楼中楼纯文本
    要进一步知道楼中楼回复关系建议用get_comments接口


    IP属地:广东2楼2024-04-21 11:08
    收起回复