尝试通过 URL 获取文件时如何避免出现错误 403: Forbidden

问题描述 投票:0回答:1

我有一个连接到

MySQL database
的 Discord.py 机器人。 我想根据用户的
URL
从数据库中检索用户的横幅
Discord user ID
。 检索 URL 后,我使用
urllib.request.urlretrieve
下载图像并将其保存到
local file
。 尝试下载图像时,我收到 urllib.error.HTTPError 或 urllib.error.URLError 以及 403(禁止)错误。

@commands.slash_command(name="profile", description="Fetch user data")
async def profile(self, interaction: discord.Interaction, user: discord.User):

    cursor.execute("SELECT banner_url FROM user_ui WHERE user_id = %s", (user.id,))
    result = cursor.fetchone()

    now = datetime.datetime.now()
    if result:

        print(result)
        url = result[0]
        full_path = "assets/banner.jpg"
        urllib.request.urlretrieve(url, full_path)

        banner = "assets/banner.jpg" 
    
0.09 13:49:59 [Bot] discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: HTTPError: HTTP Error 403: Forbidden
  • 我已确保MySQL数据库连接正常工作。
  • Pycord 机器人按其他命令和功能的预期运行。
  • 我已检查该 URL,它似乎是有效的。
  • 我怀疑我从 URL 下载和保存图像的方式可能存在问题。

我想了解为什么在尝试下载图像时收到 403 错误以及如何解决该错误。如果我的代码片段需要任何改进或更正,请提供指导。

python image python-requests discord.py urllib
1个回答
0
投票

我玩了一下,找到了解决方案,先保存它,然后再存储到数据库中,然后在再次使用 ut 时匹配名称

© www.soinside.com 2019 - 2024. All rights reserved.