Выходит ошибка character maps to

Выходит ошибка charmap codec can't encode character '\U0001f48e' in position 7000: character maps to

from requests import get
import json

def NFT(textas):
    text = '''<!DOCTYPE html PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
                   <html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ton Scan</title>
<style>
body{
background: #ffffff;
}
</style>
 
<body>
    '''
    text3 = '</body></html>'
    wer = ""
    ton = get(f'https://tonapi.io/v1/nft/searchItems?owner={textas}&include_on_sale=true&limit=1000&offset=0', stream=True).text
    data = json.loads(ton)
    if data["nft_items"] != []:
        my_file = open(f"{textas}.html", "w+")
        for i in range(99):
            try:
                aw = data["nft_items"][i]["metadata"]["image"].split("//")
                if aw[0] == "ipfs:":
                    wer = wer + '<img src="'+"https://gateway.ipfs.io/ipfs/"+aw[1]+f'''"width="512" height="512">
                   <h1>{data["nft_items"][i]["metadata"]["name"]}<h1>
                   <h2>{data["nft_items"][i]["collection"]["name"]}<h2>
                   <h3>==================================================<h3>'''
                else:
                    wer = wer + '<img src="'+data["nft_items"][i]["metadata"]["image"]+f'''"width="512" height="512">
                   <h1>{data["nft_items"][i]["metadata"]["name"]}<h1>
                   <h2>{data["nft_items"][i]["collection"]["name"]}<h2>
                   <h3>==================================================<h3>'''
                #print(str(i)+" "+data["nft_items"][i]["collection"]["name"]+"\n"+data["nft_items"][i]["metadata"]["name"]+"\n"+data["nft_items"][i]["metadata"]["image"]+"\n=======================")
            except:
                ...

        wer = wer + text3
        wer = text + wer
        my_file.write(wer)
        my_file.close()
    else:
        return False


NFT("EQCtiv7PrMJImWiF2L5oJCgPnzp-VML2CAt5cbn1VsKAxLiE")

Искал в Google, не нашел ответа.


Ответы (1 шт):

Автор решения: Сергей Кох

Чтоб избавиться от этой ошибки добавьте encoding="utf-8":

my_file = open(f"{textas}.html", "w+", encoding="utf-8")

Вместо try/except можно применить with open. Модуль json импортировать излишне, так как в requests встроен метод обработки таких файлов.

ton = get(f'https://tonapi.io/v1/nft/searchItems?owner={textas}&include_on_sale=true&limit=1000&offset=0', stream=True)
data = ton.json()
print(type(data))

Все-таки видите, что плохо искали - ответы уже были.

→ Ссылка