Requests 爬取乱码问题

2018-12-16  本文已影响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')
上一篇下一篇

猜你喜欢

热点阅读