首页 > 解决方案 > 帕菲属性错误

问题描述

我想做一个音乐不和谐机器人,但是当我尝试用 pafy 获取 youtube 视频时,我遇到了一些错误和异常,我不明白为什么,我只需要 pafy 部分的帮助。

这是我的代码:

import discord
import os
import pafy
import vlc
import nacl
import re

client = discord.Client()


@client.event
async def on_ready():
  print('connecter en tant que {0.user}'
  .format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
    return

  if message.content.startswith('https://www.youtube.com/'):
    print(message.content)
    video_ids = re.findall(r"watch\?v=(\S{11})", message.content)
    print(video_ids[0])
    #url = message.content
    video = pafy.new(video_ids[0])
    audio = video.getbestaudio()
    playurl = audio.url
    player = vlc.Instance.media_player_new()
    Media = vlc.Instance.media_new(playurl)
    Media.get_mrl()
    player.set_media(Media)
    player.play()
  elif message.content.startswith('$join'):
    channel = message.author.voice.channel
    await channel.connect()
  elif message.content.startswith('$bye'):
    channel = message.guild.voice_client
    await channel.disconnect()

这是错误消息:

`    Ignoring exception in on_message
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 26, in on_message
    video = pafy.new(video_ids[0])
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pafy/pafy.py", line 124, in new
    return Pafy(url, basic, gdata, size, callback, ydl_opts=ydl_opts)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pafy/backend_internal.py", line 42, in __init__
    super(InternPafy, self).__init__(*args, **kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pafy/backend_shared.py", line 97, in __init__
    self._fetch_basic()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pafy/backend_internal.py", line 50, in _fetch_basic
    allinfo = get_video_info(self.videoid, self.callback)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/pafy/backend_internal.py", line 271, in get_video_info
    sts = re.search(r'sts"\s*:\s*(\d+)', embed_webpage).group(1)
AttributeError: 'NoneType' object has no attribute 'group'    `

我该如何解决?

标签: python

解决方案


推荐阅读