D
D
Danila2019-12-08 15:42:02
Python
Danila, 2019-12-08 15:42:02

How to read XML file using JSON in Python?

Good afternoon.
There is a URL:
Inside an XML file, I try to read it using JSON in Python and nothing happens. I spent about an hour on Google and none of the instructions work.
Here is my code:

import requests
import json
content = requests.get("http://rzhunemogu.ru/RandJSON.aspx?CType=1")
json = json.loads(content.content)

Here is the error:
Traceback (most recent call last):
File "/Users/danilavoronov/Documents/Python/myBot/test.py", line 4, in
json = json.loads(content.content)
File "/Library/Developer/CommandLineTools/Library /Frameworks/Python3.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 343, in loads
s = s.decode(detect_encoding(s), 'surrogatepass')
UnicodeDecodeError: 'utf-8 ' codec can't decode byte 0xce in position 12: invalid continuation byte

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
ScriptKiddo, 2019-12-08
@Dan9I

The response is JSON, but the line breaks in it are not escaped.
Screening manually

import requests
import json
content = requests.get("http://rzhunemogu.ru/RandJSON.aspx?CType=1")
response_json = json.loads(content.text.replace('\r\n', '\\r\\n'))
print(response_json)

OUT
{'content': 'Смотpишь по телевизоpу pекламу - в ней pекламиpуют жуpнал. Покупаешь жуpнал - а там pеклама магазина. Идёшь в магазин - а там пpодают телевизоpы. Покупаешь телевизоp, включаешь - а там опять pеклама того же жуpнала. Где же выход?!'}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question