美文网首页
Requests 爬取乱码问题

Requests 爬取乱码问题

作者: 我是来捕鱼的 | 来源:发表于2018-12-16 10:05 被阅读2次

    可以用.decode('utf-8','ignore')来解码,.encode()来编码

    requests中的text()返回的是处理过的Unicode数据,而content()返回的是bytes型原始数据。所以获取网页数据时使用text(),获取照片时使用content()

    简单粗暴的方法 requests.encoding()

    网上一种终极解决方法(先mark)

    if req.encoding == 'ISO-8859-1':
        encodings = requests.utils.get_encodings_from_content(req.text)
        if encodings:
            encoding = encodings[0]
        else:
            encoding = req.apparent_encoding
    encode_content = req.content.decode(encoding, 'replace').encode('utf-8', 'replace')
    

    相关文章

      网友评论

          本文标题:Requests 爬取乱码问题

          本文链接:https://www.haomeiwen.com/subject/tdcfdftx.html