Python爬网页,不确定网页的编码,不需要用第三方库

Python爬网页,不确定网页的编码,不需要用第三方库,自己写个判断,乱拳打死老师傅

detect试了,不好用

apparent_encoding试了,不好用

encoding试了,不好用

headers里get试了,不好用

最后用下面这个方法,反而是最好用的,也不是100%准确,但是在我用的过程中,这个是最好用的,反正也没有100%的,还不如用自己写的函数呢

python 复制代码
def detect_encoding(byte_text):  
    encodings_to_try = ['utf-8', 'gbk', 'gb2312', 'gb18030', 'ascii', 'latin1']  
    for encoding in encodings_to_try:  
        try:  
            return encoding  
        except UnicodeDecodeError:  
            continue  
    return None
相关推荐
Warson_L1 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅1 天前
海天线算法的前世今生
python·计算机视觉
韩师傅1 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L1 天前
LangGraph的MessageState and HumanMessage
python
韩师傅1 天前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L1 天前
python的类&继承
python
Warson_L1 天前
类型标注/type annotation
python
ThreeS1 天前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵1 天前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏