值我想:屬性集而不LXML XML
<div data-a>
但LXML API似乎給我的只有這一點:
<div data-a=''>
我如何獲得價值少的屬性?
令人討厭的是,空值和空值由LXML表示爲空字符串。
設置None值沒有幫助。
In [19]: from lxml.html import fromstring, tostring
In [20]: b = fromstring('<body class="meow" data-a="haha" data-b data-x="">text-fef27e87389e466fb99b5421629323f6</body>')
In [21]: b.attrib
Out[21]: {'data-a': 'haha', 'data-x': '', 'data-b': '', 'class': 'meow'}
In [22]: b = fromstring('<body class="meow" data-a="haha" data-b data-x="">text-fef27e87389e466fb99b5421629323f6</body>')
In [23]: b.attrib
Out[23]: {'data-a': 'haha', 'data-x': '', 'data-b': '', 'class': 'meow'}
In [24]: b.attrib['data-y'] = None
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-1f55133e3dc4> in <module>()
----> 1 b.attrib['data-y'] = None
/usr/lib/python2.7/dist-packages/lxml/etree.so in lxml.etree._Attrib.__setitem__ (src/lxml/lxml.etree.c:58775)()
/usr/lib/python2.7/dist-packages/lxml/etree.so in lxml.etree._setAttributeValue (src/lxml/lxml.etree.c:19025)()
/usr/lib/python2.7/dist-packages/lxml/etree.so in lxml.etree._utf8 (src/lxml/lxml.etree.c:26460)()
TypeError: Argument must be bytes or unicode, got 'NoneType'
tag.attrib['data-a'] = None
TypeError: Argument must be bytes or unicode, got 'NoneType'
請出示一個示範最少的代碼。 [問] – boardrider