【发布时间】:2021-05-19 02:08:58
【问题描述】:
这就是我正在做的事情。
我正在向 reddit oembed 端点发送 get 请求。我想解析返回的 json 并获取原始 html 以将 reddit 帖子嵌入到我的 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。 我在想也许我必须重新格式化它?有人可以帮我吗?谢谢!
'\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&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&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"
}
【问题讨论】:
-
你能打印endpoint.Json()并分享结果吗?
-
编辑完成
-
你的key"html"的值不是""括起来的字符串,这是问题,你需要在使用json解码器之前解析它
-
有没有一种方法可以格式化用双引号括起来的值?
-
另外,endpoint.json()['html'] 返回的数据类型是字符串。更奇怪的是,如果我运行这段代码来查看原始 HTML,我会收到 JSON 解码错误,但在运行 7 - 8 次后,我终于收到了我想要的 html 代码。