lxml.htmlset error“TypeError:参数必须是bytes或unicode,得到'NoneType'”

2024-10-06 07:34:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用lxml.html但我明白了

TypeError:参数必须是字节或unicode,得到“NoneType”

或者

TypeError:set()只接受2个位置参数(给定1个)

如果我使用

script.set('defer')

根据http://lxml.de/lxmlhtml.html

.set(键,值=无): 设置HTML属性。如果没有给定值,或者值为None,它将创建一个布尔属性,例如

^{pr2}$

或者

<div custom-attribute></div>.

我的代码是

import lxml.html  

htmldoc = lxml.html.parse( file )   
headElement = htmldoc.find("head")
listOfScripts = headElement.xpath("script")

if len(listOfScripts) > 0:
    for script in listOfScripts:
        script.set('defer', value=None)

Tags: divnone参数字节属性htmlunicodescript
1条回答
网友
1楼 · 发布于 2024-10-06 07:34:48

您链接的文档是针对lxml的最新版本,您所说的关于.set()的功能是在版本3.7中添加的,但是您的版本是<;3.7

查看变更日志here

3.7.0 (2016-12-10)

Features added

• GH#201: Calling the method .set('attrname') without value argument (or None) on HTML elements creates an attribute without value that serialises like attrname>. Patch by Daniel Holth.

相关问题 更多 >