Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract data along with html tag when data is given as search item

I am using beautifulsoup to extract html data. I need to extract the html tags along with the data if data is given as search item provided the tag can be anything.

As a sample considering the following html code

    <h1>Hello</h1>
    <h1>Python Program</h1>
 
   <span class = true>Geeks</span>
   <span class = false>Geeks New</span>
 
   <li class = 1 >Python Program</li>
   <li class = 2 >Python Code</li>
   <li class = 3 >Hello</li>
 
   <table>
       <tr>Website</tr>
   </table>

Using the following code if tag is known, then the entire tag with data is available

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

pattern = 'Hello'
text1 = soup.find_all('li', text = pattern)
print(text1)

This will give the

[<li class = 3 >Hello</li>]

But if I give ‘Hello’ as search item I need to get all the tags which contain ‘Hello’ like

[<h1>Hello</h1>, <li class = 3 >Hello</li>]

>Solution :

You can use boolean instead of li tag

html = '''
  <h1>Hello</h1>
    <h1>Python Program</h1>
 
   <span class = true>Geeks</span>
   <span class = false>Geeks New</span>
 
   <li class = 1 >Python Program</li>
   <li class = 2 >Python Code</li>
   <li class = 3 >Hello</li>
 
   <table>
       <tr>Website</tr>
   </table>
'''
pattern = 'Hello'
soup = BeautifulSoup(html, "html.parser")

text1 = soup.find_all(True, text = pattern)
print(text1)

Output:

[<h1>Hello</h1>, <li class="3">Hello</li>]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading