gpt4 book ai didi

python - 使用 Python 3.x 从网站中提取 JSON 数据

转载 作者:行者123 更新时间:2023-12-05 07:51:12 33 4
gpt4 key购买 nike

我正在尝试从网站提取一些 JSON 数据,用于我为 Discord 机器人制作的一个小程序。代码目前看起来像:

if message.content.startswith("!jsontest"):
import requests
data = requests.get('http://schoolido.lu/api/cards/799/').json()
print(data)

我也尝试过使用 urllib.request.urlopen,但无论使用哪种方法,我都会收到 403 错误。这是输出:

   Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\site-packages\discord\client.py", line 259, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:/Users/user/Desktop/Bot.py", line 177, in on_message
response = ur.urlopen("http://schoolido.lu/api/cards/799/")
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 162, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 471, in open
response = meth(req, response)
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 581, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 509, in error
return self._call_chain(*args)
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 443, in _call_chain
result = func(*args)
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 589, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

是我遗漏了什么,还是代码有误?我已经尝试了很多来自 SO 的其他示例,但它们都没有做任何不同的事情,即我仍然得到 403

最佳答案

首先尝试将其转换为 Python 字典,如下所示。它应该有效。

if message.content.startswith("!jsontest"):
import requests
import json
data = requests.get('http://schoolido.lu/api/cards/799/')
text = data.text
dictionary = json.loads(text)
print(data)

关于python - 使用 Python 3.x 从网站中提取 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35237175/

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