zl程序教程

您现在的位置是:首页 >  其他

当前栏目

SerialisationError: IO_ENCODER 报错解决方法

2023-04-18 13:11:36 时间

SerialisationError: IO_ENCODER 报错解决方法

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

---------------------------------------------------------------------------
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

解决方法:

result = etree.tostring(html)

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

结果如下: