etree.html 中文乱码,[三脚猫指路]请求+etree+中文乱码解决方案,requestsetree,出现,的,方式...

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

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值