需要用到的工具:cChardet
每个网页都有<meta http-equiv="content-type" content="text/html; charset=UTF-8">
这个标签
html5写法
XHTML写法
方法一
import cchardet import requests url = 'https://www.99zyku.com' rs = requests.get(url) rs.encoding=cchardet.detect(rs.content)['encoding'] html = rs.text print(html)
方法二
忽略错误
import cchardet import requests url = 'https://www.99zyku.com' rs = requests.get(url) encoding = cchardet.detect(rs.content)['encoding'] html = rs.content.decode(encoding,errors='ignore') print(html)
方法三
import cchardet import requests import re url = 'https://www.99zyku.com' rs = requests.get(url) encoding = cchardet.detect(rs.content)['encoding'] ok_encode = re.compile('gbk|utf-8|gb2312',re.I) encoding = encoding if ok_encode.search(encoding) else 'utf-8' html = rs.content.decode(encoding,errors='ignore') print(html)