解决:TypeError: Type ‘list’ cannot be serialized


解决:TypeError: Type ‘list’ cannot be serialized





背景

在使用之前的代码时,报错:
Traceback (most recent call last):
File “E:/Test/test.py”, line 14, in
d = etree.tostring(divs)
File “src/lxml/etree.py”, line 3443, in lxml.etree.tostring
TypeError: Type ‘list’ cannot be serialized.



报错问题


Traceback (most recent call last):
  File "E:/Test/test.py", line 14, in <module>
    d = etree.tostring(divs)
  File "src/lxml/etree.py", line 3443, in lxml.etree.tostring
TypeError: Type 'list' cannot be serialized.



报错翻译

主要报错信息内容翻译如下所示:


Traceback (most recent call last):
  File "E:/Test/test.py", line 14, in <module>
    d = etree.tostring(divs)
  File "src/lxml/etree.py", line 3443, in lxml.etree.tostring
TypeError: Type 'list' cannot be serialized.

翻译:

追溯(最近一次通话):
文件“E:/Test/Test.py”,第14行,在中
d=etree.tosttring(div)
文件“src/lxml/etree.py”,第3443行,在lxml.etree.tostring中
TypeError:无法序列化类型“list”。



报错位置代码


...
        divs = html.xpath('//div[@class="rank"]//span[@class="span"]') 
		d = etree.tostring(divs,encoding='utf-8').encode('utf-8')
...



报错原因

经过查阅资料,发现是html.xpath()返回值是一个list类型的数据,不能被序列化引起的,然后就会出现这样的提示。

小伙伴们按下面的解决方法即可解决!!!



解决方法

要解决这个错误,需要把list转化为可序列化的值即可,常用的解决办法可以在规则末尾加 /text(),代码如下。

正确的代码是:


...
	html = etree.HTML(content)#HTML网页
	divs = html.xpath('//div[@class="rank"]//span[@class="span"]/text()')#XPATH提取数据
	print(divs)#输出数据
...

HTML()函数的官方文档内容如下:

HTML(text, parser=None, base_url=None) Parses an HTML document from a
string constant. Returns the root node (or the result returned by a
parser target). This function can be used to embed “HTML literals”
in Python code.

To override the parser with a different HTMLParser you can pass it to
the parser keyword argument.

The base_url keyword argument allows to set the original base URL of
the document to support relative Paths when looking up external
entities (DTD, XInclude, …).

翻译如下:

HTML(text, parser=None, base_url=None)
从字符串常量解析HTML文档。返回根节点(或解析器目标返回的结果)。此函数可用于在Python代码中嵌入“HTML文字”。

要用一个不同的HTMLParser覆盖解析器,你可以将它传递给parser关键字参数。

base_url关键字参数允许设置文档的原始基本URL,以便在查找外部实体(DTD、XInclude等)时支持相对路径。

xpath()函数的官方文档内容如下:

xpath(self, _path, namespaces=None, extensions=None,
smart_strings=True, **_variables) XPath evaluate in context of
document.

namespaces is an optional dictionary with prefix to namespace URI
mappings, used by XPath. extensions defines additional extension
functions.

Returns a list (nodeset), or bool, float or string.

In case of a list result, return Element for element nodes, string for
text and attribute values.

Note: if you are going to apply multiple XPath expressions against the
same document, it is more efficient to use XPathEvaluator directly.

翻译如下:

xpath(self, _path, namespaces=None, extensions=None,
smart_strings=True, **_variables) XPath在文档上下文中求值。

namespaces是一个可选的字典,带有名称空间URI映射的前缀,用于XPath。Extensions定义了额外的扩展函数。

返回一个列表(节点集),或bool、float或string。

如果是列表结果,则对元素节点返回Element,对文本和属性值返回string。

注意:如果要对同一文档应用多个XPath表达式,那么直接使用XPathEvaluator会更有效。以上翻译结果来自有道神经网络翻译(YNMT)·
通用场景



今天的分享就到此结束了

欢迎点赞评论关注三连

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ninghes

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

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

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

打赏作者

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

抵扣说明:

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

余额充值