错误:json.decoder.JSONDecodeError:期望值:第1行第1列(字符0)

时间:2019-08-29 06:34:17

标签: python

我想通过API从以下网址获得一些数据:https://api.hooktheory.com/v1/users/auth

我可以用键输入它,但是由于以下错误,我想请求一些数据后将无法使用。

我已经通过https://bootstrap.pypa.io/get-pip.py下载了pip并导入了请求,我也通过conda install pip进行了尝试。什么都没有解决,问题仍然存在。我已经在这里搜索了一些解决方案,但是已经找到了。不是重复的。仅供参考:我在带有Visual Studio的Mac OS X上工作。

import requests
import time

login = {"Accept": "application/json", 
      "Content-Type": "application/json",
      "username":"huks",
      "password": "XXXX"}


url = "https://api.hooktheory.com/v1/users/auth"
r = requests.post(url, data=login)
print(r.json())

time.sleep(5)

activkey = 'XXXX'
header = {"Authorization": "Bearer " + activkey}

r = requests.get(url+'trends/songs', headers=header)
r.json()

r = requests.get(url+'trends/nodes?cp=4', headers=header)
r.json()

这是回溯+错误消息:

File "/Users/marius/Desktop/INNOLAB/tempCodeRunnerFile.py", line 20, in <module>
    r.json()
  File "/Users/marius/anaconda3/lib/python3.7/site-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/Users/marius/anaconda3/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/Users/marius/anaconda3/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/marius/anaconda3/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

1 个答案:

答案 0 :(得分:0)

import requests
import time

login = {"Accept": "application/json", 
      "Content-Type": "application/json",
      "username":"huks",
      "password": "XXXX"}


url = "https://api.hooktheory.com/v1/users/auth"
r = requests.post(url, data=login)
print(r.json())

time.sleep(5)

activkey = 'XXXX'
header = {"Authorization": "Bearer " + activkey}

r = requests.get(url+'/trends/songs', headers=header)
r.json()

r = requests.get(url+'/trends/nodes?cp=4', headers=header)
print(r.text) #this will print what is the response you got!
if r.status_code == 200:
    print(r.json()) #this will work only if response is JSON

希望评论有意义!