两种方法:
import chardet, requests, urllib
from urllib.request import urlopen
url = 'https://www.liaoxuefeng.com/wiki ... 00/1183255880134144'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 '
'Core/1.63.6823.400 QQBrowser/10.3.3117.400'}
res = requests.get(url, headers = headers)
s = res.encoding
print(s)
resp = urlopen('http://www.baidu.com')
html = resp.read()
chardet1 = chardet.detect(html)
m = chardet1['encoding']
print(m)
结果:
UTF-8
utf-8
Process finished with exit code 0
第二种方法用你原来的网址,报错503