SerialisationError: IO_ENCODER 报错解决方法
在将 xpath 的 element 元素,转换成 html 源码,报了以下错误:
---------------------------------------------------------------------------SerialisationErrorTraceback (mostrecentcalllast) <ipython-input-12-4cbcdb81e506>in<module>1spider=BiliSpider("BV16p4y187hc") ---->2spider.run() <ipython-input-11-286c53a34004>inrun(self) 37defrun(self): 38# 1.根据BV号获取网页内容--->39html_content=self.getHTML_content() 40# 2.请求并获取script数据41script_list=self.get_script_list(html_content) <ipython-input-11-286c53a34004>ingetHTML_content(self) 11html_str=response.content.decode() 12html=etree.HTML(html_str) --->13result=etree.tostring(html) 14returnresult15src/lxml/etree.pyxinlxml.etree.tostring() src/lxml/serializer.pxiinlxml.etree._tostring() src/lxml/serializer.pxiinlxml.etree._raiseSerialisationError() SerialisationError: IO_ENCODER
解决方法:
result=etree.tostring(html) 改为如下,加上encoding="utf-8"result=etree.tostring(html, encoding="utf-8")
结果如下: