url = "" headers = { "Host":"www.chinanews.com", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36", "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Encoding":"gzip, deflate", "Accept-Language":"zh-CN,zh;q=0.9", } try: proxies =get_proxies() result = session.get(url=url,headers=headers).content except: proxies = get_proxies() result = session.get(url=url,headers=headers).content import chardet print(chardet.detect(result))
核心代码
soup = BeautifulSoup(result,'html.parser',fromEncoding="GBK")
result_replace = replace(soup)
print result_replace
print(chardet.detect(result_replace))
输出结果: