为什么我会收到此 JSON 解码错误?

Why am I receiving this JSON Decode Error?

这是我正在做的事情。

我正在向 reddit oembed 端点发送获取请求。我想解析 returned json 并获取原始 html 以将 reddit post 嵌入到我的 django 页面上。我尝试执行此操作时收到的错误是

json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1)

下面是该代码的一个示例。 (它在函数内部)

 endpoint = requests.get("https://www.reddit.com/oembed?url=https://www.reddit.com/r/nba/comments/n6l2zu/the_crew_lock_in_their_predictions_and_ernie_has/")

 return endpoint.json()['html']

这里是 html 它应该 return。 我在想也许我必须重新格式化它?有人可以帮我吗?谢谢!

 '\n    <blockquote class="reddit-card" >\n      <a href="https://www.reddit.com/r/nba/comments/n6l2zu/the_crew_lock_in_their_predictions_and_ernie_has/?ref_source=embed&amp;ref=share">The crew lock in their predictions and Ernie has the Jazz going to the Finals</a> from\n      <a href="https://www.reddit.com/r/nba/">nba</a>\n    </blockquote>\n    <script async src="https://embed.redditmedia.com/widgets/platform.js" charset="UTF-8"></script>\n'

编辑:

这是打印的结果 endpoint.json()

    {
   "provider_url":"https://www.reddit.com/",
   "version":"1.0",
   "title":"The crew lock in their predictions and Ernie has the Jazz going to the Finals",
   "provider_name":"reddit",
   "type":"rich",
   "html":"\n    <blockquote class=\"reddit-card\" >\n      <a href=\"https://www.reddit.com/r/nba/comments/n6l2zu/the_crew_lock_in_their_predictions_and_ernie_has/?ref_source=embed&amp;ref=share\">The crew lock in their predictions and Ernie has the Jazz going to the Finals</a> from\n      <a href=\"https://www.reddit.com/r/nba/\">nba</a>\n    </blockquote>\n    <script async src=\"https://embed.redditmedia.com/widgets/platform.js\" charset=\"UTF-8\"></script>\n",
   "author_name":"tanookiben"
}
import requests
import json

def get_response():
    endpoint = requests.get("https://www.reddit.com/oembed?url=https://www.reddit.com/r/nba/comments/n6l2zu/the_crew_lock_in_their_predictions_and_ernie_has/")
    if endpoint.status_code == 200:
        return json.loads(endpoint.text)
        
    return {}
    
print(get_response())

当您发出请求时,reddit 似乎会响应如下错误消息。所以最好先检查响应状态码。

<!doctype html>
<html>
  <head>
    <title>Too Many Requests</title>
    <style>
      body {
          font: small verdana, arial, helvetica, sans-serif;
          width: 600px;
          margin: 0 auto;
      }

      h1 {
          height: 40px;
          background: transparent url(//www.redditstatic.com/reddit.com.header.png) no-repeat scroll top right;
      }
    </style>
  </head>
  <body>
    <h1>whoa there, pardner!</h1>
    


<p>we're sorry, but you appear to be a bot and we've seen too many requests
from you lately. we enforce a hard speed limit on requests that appear to come
from bots to prevent abuse.</p>

<p>if you are not a bot but are spoofing one via your browser's user agent
string: please change your user agent string to avoid seeing this message
again.</p>

<p>please wait 6 second(s) and try again.</p>

    <p>as a reminder to developers, we recommend that clients make no
    more than <a href="http://github.com/reddit/reddit/wiki/API">one
    request every two seconds</a> to avoid seeing this message.</p>
  </body>
</html>

我没有发送正确的 header。一直返回 429 错误,因为没有返回 json,我收到 json 解码器错误。