Christian Eslabon
Christian Eslabon

Reputation: 185

How to identify if a <p> tag is inside a <table> tag using python beautifulsoup?

How to find which p tags are inside vs outside a table tag?

<p> word outside p tag inside table tag </p>

<table>
  <tbody>
    <tr>
      <td>
        <p> word inside p tag inside table tag </p>
     </td>
   </tr>
 </tbody>
</table>

Upvotes: 1

Views: 432

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195573

You can use CSS selector:

from bs4 import BeautifulSoup

html_doc = """
<p> word outside p tag inside table tag </p>

<table>
  <tbody>
    <tr>
      <td>
        <p> word inside p tag inside table tag </p>
     </td>
   </tr>
 </tbody>
</table>
"""

soup = BeautifulSoup(html_doc, "html.parser")

for p in soup.select("table p"):
    print(p.text)

Prints:

 word inside p tag inside table tag 

Or using bs4 API:

for table in soup.find_all("table"):
    for p in table.find_all("p"):
        print(p.text)

Upvotes: 1

Related Questions