W
W
wolron2020-05-28 13:21:56
Python
wolron, 2020-05-28 13:21:56

Why is JSON.loads throwing an error?

The variable contains both HTML tags and JavaScript data. It is necessary to place in the Python dictionary a part of the parsed page that looks like: "key" : "value".

This script is throwing an error for some reason.

TypeError: the JSON object must be str, bytes or bytearray, not BeautifulSoup


import requests # модуль для парсинга
from bs4 import BeautifulSoup #модуль для парсинга
import json 

s = requests.Session()
loging = s.get(URL, headers=HEADERS, params=None)
soup = BeautifulSoup(loging.content, 'html.parser')
json_dat = json.loads(soup)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Pankov, 2020-05-28
@trapwalker

Here you make an html parser object:

soup = BeautifulSoup(loging.content, 'html.parser')

and here you put it instead of a string in josn's parser:
json_dat = json.loads(soup)
Here's what the error message says about it:
TypeError: the JSON object must be str, bytes or bytearray, not BeautifulSoup

You're trying to fit an entire gas station into the car's gas tank. Doesn't climb.
You need to find a way to extract gasoline from the gas station and put it in the gas tank.

S
soremix, 2020-05-30
@SoreMix

import requests # модуль для парсинга
from bs4 import BeautifulSoup #модуль для парсинга
import json 

s = requests.Session()
loging = s.get(URL, headers=HEADERS, params=None)
# при условии, что страница JSON валидна
json_dat = json.loads(loging.text)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question