2017-06-13 39 views
0

我试图把一个“延迟”使用lxml.html属性转换脚本标签,但我得到的错误lxml.html设置错误“类型错误:参数必须是字节或Unicode,得到了‘NoneType’”

类型错误:参数必须是字节或Unicode,得到了 'NoneType'

类型错误:组()恰恰2位置参数(给定1)

如果我使用

script.set('defer') 

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

.SET(键,值=无): 设置HTML属性。如果没有给定值,或者如果值是None,它会建立像

<form novalidate></form> 

一个布尔属性
<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) 
+0

嗯,可能是它在LXML错误。文档说'None'(或者如果省略)表示设置为布尔属性。但恕我直言,你可以将其设置为“推迟”字符串值,XHTML也应该吃它 –

回答

0

您链接的文档是最新版本的lxml,你所说的.set()的功能是在3.7版本中加入的,不过你的版本是< 3.7

查看的changelog 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.

+0

是的,我的版本是3.6谢谢 –

+0

我已经更新我的lxml到3.8.0.0版本,并且它现在运行没有错误,script.set 'defer')或script.set('defer',None)或script.set('defer',value = None)都会产生

相关问题