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

时间:2016-02-06 04:00:38

标签: python json

我试图从一个网站上提取一些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

0 个答案:

没有答案