Beautifulsoup Find The Next Specific Tag Following A Found Tag
Given the following (simplified from a larger document) Age16
Solution 1:
if you get list of elements then you can use [index]
to get element from list.
data = """<tr class="row-class">
<td>Age</td>
<td>16</td>
</tr>
<tr class="row-class">
<td>Height</td>
<td>5.6</td>
</tr>
<tr class="row-class">
<td>Weight</td>
<td>103.4</td>
</tr>"""from bs4 import BeautifulSoup
soup = BeautifulSoup(data)
trs = soup.find_all("tr", {"class":"row-class"})
for tr in trs:
tds = tr.find_all("td") # you get listprint('text:', tds[0].get_text()) # get element [0] from listprint('value:', tds[1].get_text()) # get element [1] from list
result
text: Agevalue: 16text: Heightvalue: 5.6text: Weightvalue: 103.4
Post a Comment for "Beautifulsoup Find The Next Specific Tag Following A Found Tag"