zoukankan      html  css  js  c++  java
  • BeautifulSoup4 print() 输出中文乱码解决方法

    urllib.request 返回的数据需要解码,如 网站返回的是GBK编码数据. 需要调用decode("gbk") 此时输出不会乱码.
    with urllib.request.urlopen(url, context=context) as response:
                html = response.read()
                html=html.decode("gbk")
                print(html);
                print(response.code())#200是正常响应

    code

    import  requests
    from bs4 import BeautifulSoup  #pip install beautifulsoup4
    '''
    BeautifulSoup 输出中文 => print cmd 默认编码是 Codepage 936
    https://www.baidu.com/ 网页编码是 uft-8
    导致 print() 输出乱码
    解决方法:
    让 r.encoding = 'gbk2312'; 编码为 gbk 此时输出正常.
    '''
    
    #你可以找出 Requests 使用了什么编码,并且能够使用 r.encoding 属性来改变它
    r= requests.get('https://www.baidu.com/');
    r.encoding = 'gbk2312';
    soup=BeautifulSoup(r.text,"html.parser") 
    print(soup.title);
    #写入到文件.
    fo = open('ttt.txt', "w")        
    fo.write(('
    ' +soup.title.string + '
    '))  
    fo.close() 
     
     
     
     
     
     
     
     
     
     
     

  • 相关阅读:
    禅道
    centos7 安装redis 出现cc: command not found错误解决
    Linux 安装 redis
    vuex store modules
    vuex store 改造
    vuex store
    Vue axios
    Vue keep-alive
    vue 路由守卫
    vue-router 参数传递
  • 原文地址:https://www.cnblogs.com/sea-stream/p/14192109.html
Copyright © 2011-2022 走看看