新手求助
starry吧
全部回复
仅看楼主
level 1
charuty12138 楼主
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以及他对应的回复,这样我不就不需要用正则提取出来了
谢谢
2024年04月11日 13点04分 1
吧务
level 15
1.我这能搜到结果。另外gather里面只有一个对象的话就没必要用gather了
2.for post in posts: for comment in post.comments: print(comment.text)就能拿到楼中楼纯文本
要进一步知道楼中楼回复关系建议用get_comments接口
2024年04月21日 03点04分 2
感谢感谢,我后来发现是mac不行,Ubuntu就可以
2024年04月21日 03点04分
佬想问下有没有脚本墙这个东西[小乖]
2024年12月02日 12点12分
@贴吧用户_Q5US2yU 删帖机?
2024年12月03日 13点12分
@Starry 差不多吧,就是能检测几级以下发言自动删除的[委屈]
2024年12月04日 14点12分
1