在python请求中读取特殊字符时出现问题。各种失败的尝试

2024-09-25 08:25:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在堆栈中找到类似的问题,但我还没有找到解决问题的方法。 我有以下python代码

        headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
            'Accept': '*/*',
            'Accept-Language': 'pt-BR,pt;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'Accept-Encoding': 'gzip, deflate, br',
            'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
            'X-Requested-With': 'XMLHttpRequest',
            'Connection': 'keep-alive',
        }


        response = requests.request("POST", URL_ENDPOINT, headers=headers, data=payload_novo)
        print(response.text)

输出为:

{"output": "\n                        <h3 class=\"titulo-servico\">Local de vota\u00e7\u00e3o</h3>\n                        <p></p>\n                        <p>Os dados informados (nome, data de nascimento ou filia\u00e7\u00e3o) n\u00e3o conferem com aqueles constantes do Cadastro Eleitoral.</p>\n                        <p></p>\n                        <button type=\"button\" \n                            class=\"btn btn-amarelo\" \n
                  onclick=\"exibirConsultaLocalVotacao()\">\n                            Nova consulta\n                        </button>\n                        </p>\n                        ", "type": "success"}

看看他带着像"\\ u00e7 \ u00e3o"这样的角色回来。我尝试了各种方法通过解码来解码,但结果总是一样的

但是,如果我创建了一个新的python文件,并将这个返回值放入一个变量中并将其打印出来,结果将毫无问题地显示出来,也就是说,特殊字符可以正常工作

myString= '''{"output": "\n                        <h3 class=\"titulo-servico\">Local de vota\u00e7\u00e3o</h3>\n                        <p></p>\n                        <p>Os dados informados (nome, data de nascimento ou filia\u00e7\u00e3o) n\u00e3o conferem com aqueles constantes do Cadastro Eleitoral.</p>\n                        <p></p>\n                        <button type=\"button\" \n                            class=\"btn btn-amarelo\" \n
                      onclick=\"exibirConsultaLocalVotacao()\">\n                            Nova consulta\n                        </button>\n                        </p>\n                        ", "type": "success"}'''    


print(myString)

正确的输出为:

{"output": "
                        <h3 class="titulo-servico">Local de votação</h3>
                        <p></p>
                        <p>Os dados informados (nome, data de nascimento ou filiação) não conferem com aqueles constantes do Cadastro Eleitoral.</p>
                        <p></p>
                        <button type="button"
                            class="btn btn-amarelo"

                      onclick="exibirConsultaLocalVotacao()">
                            Nova consulta
                        </button>
                        </p>
                        ", "type": "success"}

请注意: 出口1段:

<h3 class=\"titulo-servico\">Local de vota\u00e7\u00e3o</h3>

出口2段:

<h3 class="titulo-servico">Local de votação</h3>


Tags: datalocaltypedebuttonh3classheaders
1条回答
网友
1楼 · 发布于 2024-09-25 08:25:13

这可能是两个问题之一:

  1. 您正在打印的响应实际上是一个JSON负载。在这种情况下,正确的解码方式不是通过text,而是通过json()

    print(response.json())
    

    或者更具体地说

    print(response.json()["output"])
    
  2. 第二件可能出错的事情是请求的自动字符集编码检测

    您可以通过打印响应的encoding字段来验证它是否正确检测到utf-8:

    print(response.encoding)
    

    如果是意外情况,可以在使用textjson字段之前显式设置:

    response.encoding = 'utf-8'
    

相关问题 更多 >