Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
specked2527 opened this issue Feb 13, 2024 · 14 comments
Closed

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

specked2527 opened this issue Feb 13, 2024 · 14 comments
Labels
question 这啥呀这是,我不到啊

Comments

@specked2527
Copy link

specked2527 commented Feb 13, 2024

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
@specked2527 specked2527 added the question 这啥呀这是,我不到啊 label Feb 13, 2024
@Nemo2011
Copy link
Owner

Ctrl + C

@Nemo2011
Copy link
Owner

@specked2527
Copy link
Author

Ctrl + C

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

2024-02-13.22-34-48.mp4

@specked2527 specked2527 changed the title [提问] {为什么无法运行? Google,issue没找到解决方法} [提问] {为什么无法运行?程序运行1分钟没有输出,无响应。 Google,issue没找到解决方法} Feb 13, 2024
@z0z0r4
Copy link
Collaborator

z0z0r4 commented Feb 14, 2024

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

@specked2527
Copy link
Author

specked2527 commented Feb 14, 2024

虽然但是,建议使用 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
Copy link
Owner

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

@Nemo2011
Copy link
Owner

不使用 vscodedebug 运行试试

@Nemo2011
Copy link
Owner

或者安装 importlib-metadata 再试试 debug

@specked2527
Copy link
Author

或者安装 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
2024-02-15.02-03-52.mp4

@Nemo2011
Copy link
Owner

发下程序

@z0z0r4
Copy link
Collaborator

z0z0r4 commented Feb 17, 2024

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

发下程序

@specked2527
Copy link
Author

发下程序

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

发下程序

程序: 1.zip

超级诡异
0e6eefcaa0265483d46fd30054a85e5c

@z0z0r4
Copy link
Collaborator

z0z0r4 commented Feb 24, 2024

我无语了...老哥你是不会打断点的吗?看看返回值啊 三个人在云 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
Copy link
Collaborator

z0z0r4 commented Feb 24, 2024

image

z0z0r4 added a commit to z0z0r4/bilibili-api that referenced this issue Feb 24, 2024
@z0z0r4 z0z0r4 mentioned this issue Feb 24, 2024
@z0z0r4 z0z0r4 changed the title [提问] {为什么无法运行?程序运行1分钟没有输出,无响应。 Google,issue没找到解决方法} [提问] 获取动态示例错误 Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question 这啥呀这是,我不到啊
Projects
None yet
Development

No branches or pull requests

3 participants