彻底解决Python3写爬虫或网站时的乱码问题

第一次写贴子,试试水

很多玩Python3的都会遇到编码问题,如果直接去处理未知编码的网页,不是Python内部编码格式utf8会出现乱码,下面介绍一种将未知编码的字串转换为utf8来避免乱码的方法,

在很多Python编码转换的场景中都可以使用,

这段是自己写的爬虫中的一段代码,代码比较简短,聪明的你一定能抓住其中的重点

#请求网页并转网页编码
def getHtmlAndDealCode(url):
    #html=requests.get(url,verify=False)
    html = s.get(url,headers=header)
    code=html.encoding
    html=html.text
    html=html.encode(code)
    html=html.decode('utf-8')
    parser = 'html.parser'
    soup = BeautifulSoup(html ,parser)
    return soup
原文地址:https://www.cnblogs.com/pozhu15/p/11306335.html