python实现自动获取网页编码

微信扫一扫,分享到朋友圈

python实现自动获取网页编码
收藏 00

需要用到的工具: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)

 

一个热爱互联网的咸鱼

你也可能喜欢

发表评论

您的电子邮件地址不会被公开。 必填项已用 * 标注

提示:点击验证后方可评论!

插入图片

热门

    抱歉,30天内未发布文章!
返回顶部