前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SerialisationError: IO_ENCODER 报错解决方法

SerialisationError: IO_ENCODER 报错解决方法

作者头像
叶庭云
发布2021-12-07 13:50:43
3380
发布2021-12-07 13:50:43
举报
文章被收录于专栏:Python进阶之路

SerialisationError: IO_ENCODER 报错解决方法

在将 xpath 的 element 元素,转换成 html 源码,报了以下错误:

代码语言:javascript
复制
---------------------------------------------------------------------------
SerialisationError                        Traceback (most recent call last)
<ipython-input-12-4cbcdb81e506> in <module>
      1 spider = BiliSpider("BV16p4y187hc")
----> 2 spider.run()

<ipython-input-11-286c53a34004> in run(self)
     37     def run(self):
     38         # 1.根据BV号获取网页内容
---> 39         html_content = self.getHTML_content()
     40         # 2.请求并获取script数据
     41         script_list = self.get_script_list(html_content)

<ipython-input-11-286c53a34004> in getHTML_content(self)
     11         html_str = response.content.decode()
     12         html = etree.HTML(html_str)
---> 13         result = etree.tostring(html)
     14         return result
     15 

src/lxml/etree.pyx in lxml.etree.tostring()

src/lxml/serializer.pxi in lxml.etree._tostring()

src/lxml/serializer.pxi in lxml.etree._raiseSerialisationError()

SerialisationError: IO_ENCODER

解决方法:

代码语言:javascript
复制
result = etree.tostring(html)

改为如下,加上encoding="utf-8"
result = etree.tostring(html, encoding="utf-8")

结果如下:

在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/10/08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档