【三脚猫指路】requests+etree+中文出现乱码的解决方式

今天记录个编码问题的解决方法(好像时不时这个编码问题就会跳出来烦一下)。

import requests
from lxml import etree

req = requests.get("https://www.cn.com/index.html")  #某网页,有中文


if req.encoding == 'ISO-8859-1':
    encodings = requests.utils.get_encodings_from_content(req.text) #这方式其实还能往下琢磨,本篇就不说了。其实自己层层print下去,外加看文档是能很好体会的。
    if encodings:
        encoding = encodings[0]
    else:
        encoding = req.apparent_encoding
    encode_content = req.content.decode(encoding, 'replace') #如果设置为replace,则会用?取代非法字符; 说白了这个req.content就是bytes的形式!!!


selector = etree.HTML(encode_content) #一定要用decode过的
content = selector.xpath('//div[@class="publish"]/div/table')[0]  #content其实是个类
content_bytes = etree.tostring(content,encoding='utf-8')  #没错这个变量也是bytes
content_str = content_bytes.decode(encoding,'replace')   #于是照抄有!!!的那行的方法

最后三行可以说是最蒙圈的。

首先content是个类,也就算了。为什么tosting这种词返回的是bytes??? 就因为这个想当然的点,耗费了一个小时(毕竟本人debug手段不行,大多靠猜想,然后靠print映证)。

然后如果直接提取text 是没有那么复杂的,直接

#selector的赋值就是上面代码片里的。
headers= selector.xpath('//div[@class="publish"]/div/table//th/text()')
print(headers)

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

取啥都被占用

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值