1

I have a piece of html code where I have to remove the hidden elements from it. I tried the following code

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()

This is not removing the hidden elements. Any idea how to remove those hidden elements

newbie
  • 1,282
  • 3
  • 20
  • 43

1 Answers1

2

Since the pyquery doesn't support the pseudo classes like :hidden, I have added my own custom class ('myhide') to use that as a selector. So I have used the following code to remove the hidden elements

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()

So the output comes like this

This is another paragraph
This is a div
newbie
  • 1,282
  • 3
  • 20
  • 43