zoukankan      html  css  js  c++  java
  • python编码问题

    linux、mac os黙认utf-8

    windows黙认GBK

    python 2

    python 2黙认使用ascii码,python 2读入文件中的字符串黙认以文件声明为准,声明的是什么就以什么做为编码

    GBK—decode('gbk')———》unicode-----encode('utf-8')-----》UTF-8

    UTF-8-----decode('utf-8')-------->unicode-----encode('gbk')------->GBK

    python3

    python 3黙认使用unicode,python 3读入文件中的字符串时,不管是什么编码,都会先转换成unicode,也就是说,在python 3中使用的字符串都是unicode编码

    python 3还有一种格式,称为bytes,用于存储和网络传输

    requests乱码问题

    如下:

    import requests
    
    #1、指定url
    url = 'https://www.baidu.com'
    
    #2、发起get请求,返回响应对象
    response = requests.get(url=url)
    
    #3、获取响应对象值 ,.text为str,content为byte
    response_text = response.text
    
    with open('./re2.html',"w",encoding="utf-8") as f:
        f.write(response_text)

    以上代码写入的re2.html将出现乱码

    原因:

    response.text将请求的网页数据黙认以'latin1'编码decode成unicode,而网页请求过来的数据是utf-8编码格式,所以response.text得到的就是乱码

    解决方案:

    1、如果 Requests 检测不到正确的编码,那么你告诉它正确的是什么

    import requests
    
    #1、指定url
    url = 'https://www.baidu.com'
    
    #2、发起get请求,返回响应对象
    response = requests.get(url=url)
    response.encoding
    = 'utf-8'

    print(type(response)) #3、获取响应对象值 ,.text为str,content为byte response_text = response.text with open('./re3.html',"w",encoding="utf-8") as f: f.write(response_text)

    2、将错误编码的unicode数据以原来错误的decode编码重新encode成bytes格式

    import requests
    
    #1、指定url
    url = 'https://www.baidu.com'
    
    #2、发起get请求,返回响应对象
    response = requests.get(url=url)
    
    #人为指定编码格式为utf-8
    # response.encoding = 'utf-8'
    
    #3、获取响应对象值 ,.text为str,content为byte,将response.text以'latin-1'编码进行encode
    response_text = response.text.encode('latin-1')
    
    with open('./re3.html',"wb") as f:
        f.write(response_text)

    3、直接使用response.content,获取bytes编码格式数据

    import requests
    
    #1、指定url
    url = 'https://www.baidu.com'
    
    #2、发起get请求,返回响应对象
    response = requests.get(url=url)
    
    #人为指定编码格式为utf-8
    # response.encoding = 'utf-8'
    
    #3、获取响应对象值 ,.text为str,content为byte
    response_content = response.content
    
    with open('./re3.html',"wb") as f:
        f.write(response_content)
  • 相关阅读:
    【Selenium IDE】下载安装Chrome和Firefox插件IDE ide了解就行 不是重点 重点是写脚本
    调用接口时,生产环境,路径加斜杠“/”和不加的区别
    WPF 踩坑笔记12 DataGrid触发选中行事件
    WPF 踩坑笔记11 线程取消
    WPF 踩坑笔记10 ListBox异步动态加载
    WPF 踩坑笔记9 直接打印
    思维的体操
    【洛谷 P4213】 【模板】杜教筛(Sum)
    【洛谷 P2257】 YY的GCD(莫比乌斯反演)
    【洛谷 P4980】 【模板】Pólya 定理
  • 原文地址:https://www.cnblogs.com/hougang/p/code.html
Copyright © 2011-2022 走看看