【问题标题】:ERROR: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)错误:json.decoder.JSONDecodeError:预期值:第 1 行第 1 列(字符 0)
【发布时间】:2020-01-02 09:12:33
【问题描述】:

我想通过 API 从这个 url 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)

【问题讨论】:

  • 查看r.status_coder.text。这可能是因为您的请求有问题,响应不是 JSON。
  • 检查您的 API 链接,我认为您在这里遇到了问题:.../authtrends/songs 添加时:url+'trends/songs'
  • Accept & Content-Type are headers not payload!?
  • @pako for r.text 发生 TypeError: 'str' object is not callable。并且 r.status_code 是 200
  • @LêTưThành API 链接和手册在此处提到:hooktheory.com/api/trends/docs

标签: python


【解决方案1】:
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

希望 cmets 有意义!

【讨论】:

  • 谢谢!我刚刚调试过,也试了一下你的代码,但是出现了同样的错误。
  • 您收到的响应文本是什么?
  • 和之前一样:json.decoder.JSONDecodeError: 期望值:line 1 column 1 (char 0)
  • @hux 伙计,你没有明白这一点。响应文本不同。你在谈论错误。 print(r.text) 打印什么?
猜你喜欢
  • 1970-01-01
  • 2019-09-10
  • 2016-03-03
  • 2020-08-16
  • 2019-06-10
  • 1970-01-01
  • 2019-11-06
  • 1970-01-01
  • 2021-04-27
相关资源
最近更新 更多