Issue
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>
Solution
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)
Answered By - Andrej Kesely
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.