首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我会收到这个JSON解码错误?

为什么我会收到这个JSON解码错误?
EN

Stack Overflow用户
提问于 2021-05-19 02:08:58
回答 2查看 956关注 0票数 0

这就是我在做的事。

我正在向reddit oembed端点发送get请求。我想解析返回的json并获取原始的html,将reddit文章嵌入到django页面上。当我尝试这样做时,我收到的错误是

代码语言:javascript
复制
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1)

下面是该代码的一个实际操作示例。(它在一个函数中)

代码语言:javascript
复制
 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。,我在想,也许我必须重新格式化它?能帮我一下吗?谢谢!

代码语言:javascript
复制
 '\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()的结果

代码语言:javascript
复制
    {
   "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"
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-05-26 05:44:09

我没有发一个合适的头球。一直得到一个429错误,因为没有返回json,所以我收到了json解码器错误。

票数 0
EN

Stack Overflow用户

发布于 2021-05-19 02:15:42

代码语言:javascript
复制
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响应错误消息。因此,最好先检查响应状态代码。

代码语言:javascript
复制
<!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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67596004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档