gpt4 book ai didi

How can I avoid getting Error 403: Forbidden when trying to get file by URL(如何避免在尝试通过URL获取文件时出现错误403:禁止)

转载 作者:bug小助手 更新时间:2023-10-25 14:43:04 31 4
gpt4 key购买 nike



I have a Discord.py bot that connects to a MySQL database.
I want to retrieve a user's banner URL from the database based on their Discord user ID.
After retrieving the URL, I use urllib.request.urlretrieve to download the image and save it to a local file.
I receive a urllib.error.HTTPError or a urllib.error.URLError with a 403 (Forbidden) error when attempting to download the image.

我有一个连接到MySQL数据库的Discord.py机器人。我希望根据用户的不一致用户ID从数据库中检索用户的横幅URL。在检索到URL之后,我使用urllib.quest.url检索来下载图像并将其保存到本地文件。我在尝试下载图像时收到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


  • I have ensured that the MySQL database connection works correctly.

  • The Pycord bot operates as expected for other commands and functionalities.

  • I have checked the URL, and it appears to be valid.

  • I suspect that there may be an issue with how I'm downloading and saving the image from the URL.


I would like to understand why I'm receiving the 403 error when attempting to download the image and how to resolve it. If there are any improvements or corrections needed in my code snippet, please provide guidance.

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


更多回答

403 generally means that the server doesn't want to talk to you. Maybe it doesn't like your IP address domain, or maybe it thinks you're a bot.

403通常意味着服务器不想与您通话。也许它不喜欢你的IP地址域,或者它认为你是一个机器人。

yeah but how can I solve it, I know what 403 means.

是的,但我怎么才能解决这个问题,我知道403是什么意思。

Are you able to set headers on the request? One common way that a site decides you're a bot is to look at the user-agent header on the request.

您是否能够设置请求的标头?站点决定您是机器人的一种常见方式是查看请求上的用户代理头。

so like: user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 OPR/102.0.0.0" url = result[0] full_path = "assets/banner.jpg" # Create a request object with a User-Agent header request = urllib.request.Request(url, headers={'User-Agent': user_agent}) with urllib.request.urlopen(request) as response: with open(full_path, 'wb') as out_file: out_file.write(response.read()) ?

例如:USER_AGENT=“Mozilla/5.0(Windows NT 10.0;Win64;X64)AppleWebKit/537.36(khtml,Like Gecko)Chrome/116.0.0.0 Safari/537.36 opr/102.0.0.0“url=Result[0]Full_Path=”Assets/banner.jpg“#使用urllib.Request.Request(url,Heads={‘User-Agents’:User_AGENT})创建一个请求对象,其中User-Agents Header=urllib.Request.urlOpen(Request)作为响应:WITH OPEN(FULL_PATH,‘WB’)as out_FILE:out_file.write(Response.Read()?

Like that, yes.

就像这样,是的。

优秀答案推荐

I played a bit with it and came to the solution to save it before storing in the database and then matching the names when I use ut again

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


更多回答

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com