【问题标题】:How to fix - urllib.error.HTTPError: HTTP Error 403: Forbidden如何修复 - urllib.error.HTTPError:HTTP 错误 403:禁止
【发布时间】:2018-04-18 13:09:31
【问题描述】:

我不明白如何解决它。 我在网上找不到任何东西。

import re
@client.event
async def on_message(message):
 if message.content.startswith(prefix + 'getimg'):
    from urllib.request import urlopen
    website = urlopen('https://nekos.life/api/hug')
    html = website.read()
    links = re.findall('"((http|ftp)s?://.*?)"', html)
    print(links)
    await client.send_message(message.channel, 'get')

【问题讨论】:

  • 403:forbidden 所以你没有身份验证或访问该站点
  • 本站没有注册
  • 看起来您可能必须将 SSLContext 传递给 urlopen 才能访问 HTTPS 页面。您应该借此机会重构您的应用程序以改用aiohttp。 urllib 不是异步库,因此将其与 discord.py 代码混合可能会导致阻塞,从而使您的程序崩溃。 aiohttp 是 discord.py 使用的异步 Web 库,因此它已经在您的计算机上。

标签: python discord discord.py urlopen


【解决方案1】:
  1. 您可能无法访问该网站,因为您需要 API 密钥或其他一些东西
  2. 不要将 urllib 与不和谐的机器人一起使用。 Urllib 是一个同步库,而 Discord 机器人是异步的,因此 urllib 会在发出请求时阻止机器人的其余部分运行 这就是为什么 aiohttp 是 Discord 机器人最好的 Web 请求库,因为它确实是异步运行的 我建议您检查一下,它非常简单且很棒

编辑:Patrick Haugh 在评论中和我之前说了同样的话。我只是在写这个答案之前没有看到它

【讨论】:

    猜你喜欢
    • 2017-05-22
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多