【问题标题】:response.text is printing only special symbols for a plain-text responseresponse.text 仅打印纯文本响应的特殊符号
【发布时间】:2021-08-11 03:41:27
【问题描述】:

GET 请求下载以下输出(使用 Chrome 开发工具检查响应):

HTML 输出

<style>
.xdebug-error {
  display:none;
}
</style>
<link rel="stylesheet" href="assets/css/xyz.css">

some numbers

<script>
$(function(){
    $('#a_gen div').html(
        '<a href="https://example.com/uid" target="_blank">click to make an account</a><div id="gen_warn">Do Not Leave This Page Until Account Is Made!</div>'
    );
});
</script>

通过response.content输出

当我将response.content 打印到控制台或文件时,我会得到如下信息:

b'\x833\x01\x00\xe4R\xa7sh\xd8\x15P\x80\x0c\n \x95\xa6\x9a\xde\xd0\xe8\xa4\x9aZ\xb6\xdc\x81\xdb\x07\xad2I\xbb5\x7f\n\xb38\xb0\xb4\x15h[\xe0\x05\xdc\x02\x0b,s\xd7\x8f|\x95:\xd7\x90<6\xb7\xb7=?\xf8\xa60\xa8~\x19\xa0\x85V\x05<\x8f{\xbft\xc4n\xe1D1\xb6\xd9\x1e\x98\xfd\x94\xea\xfb\x10\xf8\x82\xee\xfb\x02\x05\xf5\xee\x07\x9eZ\xd5}?5\x88\xcaR[\x94Zb]j\xb4\xebb[ \r\xb9NH\xb4\xe7\x07\xc3O\x07\x89h\xc6\xcd\r~\x13\xd1H&\x9fK{_\\x\xb5\x80!\xc3\xf9\xc8\x15t\x11\x04\xf3\xb9\x07\x04\xf8\x1dA`!\xa5\xa1\xd2\xbd\x0c\xfe\xf5p\xa8\xfa\xf9\xf9Y\xa5\x0e\xbb\x83\xe1\xb0F\x96\xe9T(\xb7\x1c&X\\Xp5\x9c\xef\xa8\xdf&\xf5z\xb3\xd6nf=\x10\xe4*\xfb\x88\xa5\x98\x8c\xb1\xbc\xc0\xf2\x027\x82\xe5E]\x82\xe5\x85\xceY\xe0\xb0\x100Y\xa8a`?<\xacg\x98\xcc\xae\x07g\xba~x!\x97\xb7\xc6\x0fY\xeac\xf1\x85}\x9e\xc6X\xae\x93Y\xc7n\x98\xdc\xd3a\xcc\x061\x99\x99*\xf5!\x0b\xc3\xe9\x97\x1c\x99a2\xbb\xca\xbf}\xf0\x01\x8d5\xfe\x01h\xe7h\x9e\xea}\xa3\xa9\xea\x19\xc2i}@\x154\xa5\x8e~G6x\x80\xb2\x8dt\xee\x80\xbey\xe8!K\x98\xa4\xb2Y:\x7f\x83\x16\xb0\xd7O\xd5c\xa9\xc1\x8c\xa3\x03\x0f\xd0\x0e\xd4\x0f\xf8,\xa0uR-@\x0f,p(\xe2>\x85\xd6>\xda\xab\x06$s\x85n"\xfa_\xe8&\xa2=\xc1\xd7=\xe7=\x18\x18\x03'

通过response.text输出

response.text 我得到了这个(如图所示):

原码

所有变量都已定义:

s = requests.Session()
r = s.get(url,headers = headers)
print(r.text)
            
if (r.status_code == 200):
    print("Generated Successfully")
    with open("Alt.txt", 'a') as f:
        f.write(str(r.text) + '\n')
else:
    print("BAD Request " + str(r.status_code))
    s.cookies.clear()

纯文本响应如何写入文本文件或控制台?

【问题讨论】:

  • 你能展示你的python代码吗?
  • 请同时发布您的代码。
  • 嗨@Parvat.R,用我的代码编辑了答案(顺便说一句,它不是整个代码)
  • 尝试:r.content.decode('utf-8') 而不是 r.text

标签: python python-requests


【解决方案1】:

要评估来自任意 GET 请求的响应,您应该始终评估 response.headers

带有Content-Type 键的标头告诉您一些关于MIME type 的信息,例如响应的text/htmlapplication/json,以及它的编码,例如UTF-8

在您的情况下,response.headers['Content-Type'] 的结果可能会返回 "text/html; charset=UTF-8"

所以你知道,你需要将来自UTF-8 的响应解码为Parvat. R commented by r.content.decode('utf-8')

我们可以

  • 使用response.encoding 根据响应的给定编码动态解码response.text
  • 或者我们可以简单地使用response.content 将字节作为二进制表示(例如b'\x833\x01'

由于您声称响应是文本/HTML(如在浏览器中所见),您可以简单地解码文本表示并将其附加到文本文件中:

s = requests.Session()
r = s.get(url,headers = headers)
print(r.text)
            
if (r.status_code == 200):
    print("Generated Successfully")

    # detect encoding and decode respectively
    print("Response encoding", r.encoding)
    body_text = r.text.decode(r.encoding)
    with open("Alt.txt", 'a') as f:
        f.write(str(body_text) + '\n')  # print body as string to file
else:
    print("BAD Request " + str(r.status_code))
    s.cookies.clear()

另请参阅: python requests.get() returns improperly decoded text instead of UTF-8?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 2017-03-29
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    相关资源
    最近更新 更多