试图将数据从网站加载到json文件中

2024-06-16 12:38:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将数据从网站加载到json文件中

when i try this code, nothing is wrong:

import requests
import json

url="https://leoclub-ssmc.blogspot.com/?m=1"

r=requests.get(url)
print(r.text)

this code above show the web's full page with html code without error.

but when i try to load all the code into a json file then it shows some error message

import requests
import json

url="https://leoclub-ssmc.blogspot.com/?m=1"

r=requests.get(url)
print(r.json)

上述代码显示: 响应[200]的绑定方法Response.json [程序完成]

moving on::

import requests
import json

url="https://leoclub-ssmc.blogspot.com/?m=1"

r=requests.get(url).json()
print(r)

★★现在显示此错误消息:“raise JSONDecodeError(“预期值”,s,err.value)从None开始 json.decoder.JSONDecodeError:应为值:第1行第1列(字符0)” [程序完成]

我只想在json文件中加载数据,然后在我的应用程序中具体显示它们


Tags: 文件数据httpsimportcomjsonurlget
2条回答

您使用的第三种方法是正确的:

r = requests.get(url).json()

然而,这个页面实际上并没有返回JSON,因此JSONDecodeError

如果您希望抓取页面,并将数据放入Python数据结构或JSON中,您可能应该研究用于web抓取的BeautifulSoup4Selenium之类的库

您试图阅读的网站不包含JSON数据。 请尝试此网站:url=”http://api.plos.org/search?q=title:DNA"

如果您试图解析HTML文件,请检查BeautifulsoupAPI

相关问题 更多 >