我正在尝试加载json文件并得到一个错误



jdata给出了一个错误,我只是不确定为什么它不会加载。这是输出。JSONDecodeError:期望值:第1行第1列(char 0)

import requests
import json
from bs4 import BeautifulSoup

url = 'http://hahnix.com/2021/02/16/have-a-lot-of-older-trees-2-tips-to-keep-them-healthy/'
response = requests.get(url)

print(response.status_code)
soup = BeautifulSoup(response.content,'html')

print(soup.title.text)

post = soup.find("script", {"type":"application/ld+json"}).text

try:
with open('post_data.json','a',encoding='utf-8') as outfile:
outfile.write(post)
outfile.close()
except Exception as e:
print(e)
outfile.close()
jdata = json.loads('post_data.json')

如果您希望从文件加载,那么您应该使用load函数而不是loads函数。

jdata = None
with open('post_data.json') as json_file:
jdata = json.load(json_file)

我发现是使用。text导致了这个问题。如果我在BeautifulSoup对象上使用.contents,则post会写入json文件,但仍会产生错误。我知道它与无效的转义字符有关。

import requests
import json
from bs4 import BeautifulSoup

url = 'http://hahnix.com/2021/02/16/have-a-lot-of-older-trees-2-tips-to-keep-them-healthy/'
response = requests.get(url)

print(response.status_code)
soup = BeautifulSoup(response.content,'html')

print(soup.title.text)

post = soup.find("script", {"type":"application/ld+json"}).contents

with open('post_data.json','a',encoding='utf-8') as outfile:
outfile.write(post)
outfile.close()
jdata = None
with open('post_data.json') as json_file:
jdata = json.load(json_file)

相关内容

最新更新