我有一段HTML代码,我必须从中删除隐藏的元素。我尝试了以下代码
from pyquery import PyQuery
html_data = '''
<div style="display: none;">This is a hidden div.</div>
<p>This is another paragraph.</p>
<span style="display: none;">This is a span</span>
<div>This is a div.</div>
'''
pq = PyQuery(html_data)
pq(':hidden').remove()
pq.html()
pq.remove(':hidden')
pq.html()
这不是删除隐藏的元素。知道如何删除那些隐藏的元素
答案 0 :(得分:2)
由于pyquery不支持伪类,例如:hidden,我添加了自己的自定义类('myhide')以将其用作选择器。所以我使用以下代码删除隐藏的元素
from pyquery import PyQuery
html_data = '''
<div style="display: none;" class="myhide">This is a hidden div.</div>
<p>This is another paragraph.</p>
<span style="display: none;" class="myhide">This is a span</span>
<div>This is a div.</div>
'''
pq = PyQuery(html_data)
pq('.myhide').remove()
pq.html()
所以输出就像这样
This is another paragraph
This is a div