Nemo2011 / bilibili-api

哔哩哔哩常用API调用。支持视频、番剧、用户、频道、音频等功能。原仓库地址:https://github.com/MoyuScript/bilibili-api
https://nemo2011.github.io/bilibili-api/
GNU General Public License v3.0
2.19k stars 210 forks source link

[提问] 获取动态示例错误 #681

Closed specked2527 closed 8 months ago

specked2527 commented 8 months ago

Python 版本: 3.8

模块版本: 16.2.0 Name: bilibili-api-python

运行环境: Windows


Google,issue没找到解决方法

报错

Traceback (most recent call last):
  File "C:\Users\NAS\BilibiliGetDynamics\1.py", line 34, in <module>
    sync(main())
  File "C:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\site-packages\bilibili_api\utils\sync.py", line 33, in sync
    return loop.run_until_complete(coroutine)
  File "C:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 603, in run_until_complete
    self.run_forever()
  File "C:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 570, in run_forever
    self._run_once()
  File "C:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 1823, in _run_once
    event_list = self._selector.select(timeout)
  File "C:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\selectors.py", line 323, in select
    r, w, _ = self._select(self._readers, self._writers, [], timeout)
  File "C:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\selectors.py", line 314, in _select
    r, w, x = select.select(r, w, w, timeout)
KeyboardInterrupt

运行代码


import  json
from bilibili_api import user, sync

       # 实例化
u = user.User(660303135)

async def main():
      # 用于记录下一次起点
    offset = 0

    # 用于存储所有动态
    dynamics = []

    # 无限循环,直到 has_more != 1
    while True:
          # 获取该页动态
        page = await u.get_dynamics(offset)

        if 'cards' in page:
              # 若存在 cards 字段(即动态数据),则将该字段列表扩展到 dynamics
            dynamics.extend(page['cards'])

        if page['has_more'] != 1:
                # 如果没有更多动态,跳出循环
            break

        # 设置 offset,用于下一轮循环
        offset = page['next_offset']

    # 打印动态数量
    print(f"共有 {len(dynamics)} 条动态")

# 入口
sync(main())

debug

PS C:\Users\NAS\BilibiliGetDynamics> python -m pdb 1.py
> c:\users\nas\bilibiligetdynamics\1.py(1)<module>()
-> import json
(Pdb) c

Program interrupted. (Use 'cont' to resume).
> c:\users\nas\appdata\local\programs\python\python38\lib\selectors.py(315)_select()
-> return r, w + x, []
(Pdb)

Program interrupted. (Use 'cont' to resume).
> c:\users\nas\appdata\local\programs\python\python38\lib\selectors.py(315)_select()
-> return r, w + x, []
(Pdb)

Program interrupted. (Use 'cont' to resume).
> c:\users\nas\appdata\local\programs\python\python38\lib\selectors.py(315)_select()
-> return r, w + x, []
(Pdb)

Program interrupted. (Use 'cont' to resume).
> c:\users\nas\appdata\local\programs\python\python38\lib\selectors.py(315)_select()
-> return r, w + x, []
(Pdb)

Program interrupted. (Use 'cont' to resume).
> c:\users\nas\appdata\local\programs\python\python38\lib\selectors.py(315)_select()
-> return r, w + x, []
(Pdb)

Program interrupted. (Use 'cont' to resume).
> c:\users\nas\appdata\local\programs\python\python38\lib\selectors.py(315)_select()
-> return r, w + x, []
(Pdb) ww
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "C:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\process.py", line 100, in _python_exit
    items = list(_threads_wakeups.items())
  File "C:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\weakref.py", line 434, in items
    yield key, value
  File "C:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\_weakrefset.py", line 32, in __exit__
    w._commit_removals()
  File "C:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\weakref.py", line 365, in _commit_removals
    l = self._pending_removals
KeyboardInterrupt
Nemo2011 commented 8 months ago

Ctrl + C

Nemo2011 commented 8 months ago

https://blog.csdn.net/yuan2019035055/article/details/124967928

specked2527 commented 8 months ago

Ctrl + C

不不,我描述有问题,问题是;程序运行1分钟没有输出,无响应 KeyboardInterrupt 是我手动ctrl+c

https://github.com/Nemo2011/bilibili-api/assets/124735279/249ef84e-3919-4364-9f9b-2d25f8dcef7c

z0z0r4 commented 8 months ago

虽然但是,建议使用 vscode 等编辑器来调试程序

specked2527 commented 8 months ago

虽然但是,建议使用 vscode 等编辑器来调试程序

:( 感谢你的回复 vscode debug

PS C:\Users\NAS\BilibiliGetDynamics>  & 'c:\Users\NAS\AppData\Local\Programs\Python\Python38\python.exe' 'c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '8225' '--' 'C:\Users\NAS\BilibiliGetDynamics\1.py'
E+00000.093: Error while enumerating installed packages.

             Traceback (most recent call last):
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\common\log.py", line 361, in get_environment_description
                 report("    {0}=={1}\n", pkg.name, pkg.version)
             AttributeError: 'PathDistribution' object has no attribute 'name'

             Stack where logged:
               File "C:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 193, in _run_module_as_main
                 return _run_code(code, main_globals, None,
               File "C:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 86, in _run_code
                 exec(code, run_globals)
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher\__main__.py", line 91, in <module>
                 main()
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher\__main__.py", line 21, in main
                 log.describe_environment("debugpy.launcher startup environment:")
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\common\log.py", line 369, in describe_environment
                 info("{0}", get_environment_description(header))
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\common\log.py", line 363, in get_environment_description
                 swallow_exception("Error while enumerating installed packages.")
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\common\log.py", line 215, in swallow_exception
                 _exception(format_string, *args, **kwargs)

E+00000.031: Error while enumerating installed packages.

             Traceback (most recent call last):
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\common\log.py", line 361, in get_environment_description
                 report("    {0}=={1}\n", pkg.name, pkg.version)
             AttributeError: 'PathDistribution' object has no attribute 'name'

             Stack where logged:
               File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 193, in _run_module_as_main
                 return _run_code(code, main_globals, None,
               File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 86, in _run_code
                 exec(code, run_globals)
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module>
                 cli.main()
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 415, in main
                 api.ensure_logging()
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\api.py", line 61, in ensure_logging
                 log.describe_environment("Initial environment:")
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\common\log.py", line 369, in describe_environment
                 info("{0}", get_environment_description(header))
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\common\log.py", line 363, in get_environment_description
                 swallow_exception("Error while enumerating installed packages.")
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\common\log.py", line 215, in swallow_exception
                 _exception(format_string, *args, **kwargs)

E+00000.235: Error while enumerating installed packages.

             Traceback (most recent call last):
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\common\log.py", line 361, in get_environment_description
                 report("    {0}=={1}\n", pkg.name, pkg.version)
             AttributeError: 'PathDistribution' object has no attribute 'name'

             Stack where logged:
               File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 193, in _run_module_as_main
                 return _run_code(code, main_globals, None,
               File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 86, in _run_code
                 exec(code, run_globals)
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module>
                 cli.main()
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 430, in main
                 run()
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 281, in run_file
                 log.describe_environment("Pre-launch environment:")
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\common\log.py", line 369, in describe_environment
                 info("{0}", get_environment_description(header))
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\common\log.py", line 363, in get_environment_description
                 swallow_exception("Error while enumerating installed packages.")
               File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\common\log.py", line 215, in swallow_exception
                 _exception(format_string, *args, **kwargs)

手动ctrl+c

Traceback (most recent call last):
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module>
    cli.main()
  File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "C:\Users\NAS\BilibiliGetDynamics\1.py", line 34, in <module>
    sync(main())
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\site-packages\bilibili_api\utils\sync.py", line 33, in sync
    return loop.run_until_complete(coroutine)
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 603, in run_until_complete
    self.run_forever()
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 570, in run_forever
    self._run_once()
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 1823, in _run_once
    event_list = self._selector.select(timeout)
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\selectors.py", line 323, in select
    r, w, _ = self._select(self._readers, self._writers, [], timeout)
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\selectors.py", line 314, in _select
    r, w, x = select.select(r, w, w, timeout)
KeyboardInterrupt
Nemo2011 commented 8 months ago

这问题貌似与模块无关,因为问题出在 .vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64 中,估计是这里面代码的问题

Nemo2011 commented 8 months ago

不使用 vscodedebug 运行试试

Nemo2011 commented 8 months ago

或者安装 importlib-metadata 再试试 debug

specked2527 commented 8 months ago

或者安装 importlib-metadata 再试试 debug

:( 感谢你的回复 python.exe -m pip install --upgrade pip pip install importlib-metadata 没有解决问题 在vscode 里运行,20秒无输出后ctrl+c DEBUG

PS C:\Users\NAS\BilibiliGetDynamics>  c:; cd 'c:\Users\NAS\BilibiliGetDynamics'; & 'c:\Users\NAS\AppData\Local\Programs\Python\Python38\python.exe' 'c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '13084' '--' 'C:\Users\NAS\BilibiliGetDynamics\1.py'
Traceback (most recent call last):
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module>
    cli.main()
  File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 430, in main
    run()
  File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:\Users\NAS\.vscode\extensions\ms-python.debugpy-2024.0.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "C:\Users\NAS\BilibiliGetDynamics\1.py", line 34, in <module>
    sync(main())
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\site-packages\bilibili_api\utils\sync.py", line 33, in sync
    return loop.run_until_complete(coroutine)
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 603, in run_until_complete
    self.run_forever()
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 570, in run_forever
    self._run_once()
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 1823, in _run_once
    event_list = self._selector.select(timeout)
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\selectors.py", line 323, in select
    r, w, _ = self._select(self._readers, self._writers, [], timeout)
  File "c:\Users\NAS\AppData\Local\Programs\Python\Python38\lib\selectors.py", line 314, in _select
r, w, x = select.select(r, w, w, timeout)
KeyboardInterrupt

https://github.com/Nemo2011/bilibili-api/assets/124735279/dd1e34d0-fe51-491f-be6e-ad95edd1a314

Nemo2011 commented 8 months ago

发下程序

z0z0r4 commented 8 months ago

好抽象…我一直没电脑,干盯着,最顶上那份示例真的能跑吗?不会一直 while True 吧?

发下程序

specked2527 commented 8 months ago

发下程序

好抽象…我一直没电脑,干盯着,最顶上那份示例真的能跑吗?不会一直 while True 吧?

发下程序

程序: 1.zip

超级诡异 0e6eefcaa0265483d46fd30054a85e5c

z0z0r4 commented 8 months ago

我无语了...老哥你是不会打断点的吗?看看返回值啊 三个人在云 dev.jpg

image

以及B站脑瘫设计 has_more 始终为 1,所以一直在循环,还得检测下有没有 card 字段

import json
from bilibili_api import user, sync

# 实例化
u = user.User(660303135)

async def main():
    # 用于记录下一次起点
    offset = 0

    # 用于存储所有动态
    dynamics = []

    # 无限循环,直到 has_more != 1
    while True:
        # 获取该页动态
        page = await u.get_dynamics(offset)

        if "cards" in page:
            # 若存在 cards 字段(即动态数据),则将该字段列表扩展到 dynamics
            dynamics.extend(page["cards"])
        else:
            break

        if page["has_more"] != 1:
            # 如果没有更多动态,跳出循环
            break

        # 设置 offset,用于下一轮循环
        offset = page["next_offset"]

    # 打印动态数量
    print(f"共有 {len(dynamics)} 条动态")

# 入口
sync(main())
z0z0r4 commented 8 months ago

image