M
M
Magic Code2020-08-24 14:25:23
Python
Magic Code, 2020-08-24 14:25:23

Why does it throw json.decoder.JSONDecodeError: Invalid \uXXXX?

Good afternoon!
Here is the code:

import urllib.parse
import json
import requests

class TelegramMessage:
    def __init__(self, searching_info, page):
        self.searching_info = urllib.parse.quote(searching_info)
        self.page = page

    def write_in_file(self):
        with open('telegram_msg.doc', 'w') as file:
            for page in range(self.page):
                try:
                    url = f'https://search.buzz.im/api/v1/search/messages_simple?query_text=' \
                          f'{self.searching_info}&query_size=100&query_from={page}'
                    r = requests.get(url)
                    messages = json.loads(r.text)['data']['messages']
                    channels_from_messages = [x['channel'] for x in messages]
                    [file.write('@' + items + ', ')
                     for items in channels_from_messages]
                except KeyError as e:
                    print(e)


parsing = TelegramMessage(searching_info='ruby', page=10)
parsing.write_in_file()

The same code that works but without the class:
searching_info = "привет"
searching_info = urllib.parse.quote(searching_info)
for i in range(0, 1):
    url = 'https://search.buzz.im/api/v1/search/messages_simple?query_text=' + searching_info + '&query_size=100&query_from=' + str(i)
    r = requests.get(url)
    messages = json.loads(r.text)['data']['messages']
    channels_from_messages = [x['channel'] for x in messages]
    [print(x) for x in channels_from_messages]

Tell me what's the problem?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2020-08-24
@Panda_Code

Working code. This is most likely a problem on the server. At first I had a UnicodeDecodeError. After a couple of minutes, the error disappeared, and nicknames were written to the file. Then, to be sure, I copied your code again, and it turned out to be working!
Then I pasted the url into the browser - https://search.buzz.im/api/v1/search/messages_simp... and the browser returned me a blank page. Now even the browser returns json.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question