How to beautifulsoup in this case without class or id

Viewed 24

How to get the text of 'Wow, you get it!' i can print the Date, but i cant get the td that come next of the date.

<table border="0" cellpadding="4" cellspacing="1" width="100%">
  <tr bgcolor="#505050">
    <td class="white" colspan="2">
      <b>
        Account Here
      </b>
    </td>
  </tr>
  <tr bgcolor="#F1E0C6">
   <td colspan="2">
    There is nothing
   </td>
  </tr>
 </table>
 <br/>
 <br/>
 <table border="0" cellpadding="4" cellspacing="1" width="100%">
  <tr bgcolor="#505050">
   <td class="white" colspan="2">
    <b>
     Death
    </b>
   </td>
  </tr>
  <tr bgcolor="#F1E0C6">
   <td valign="top" width="25%">
     Aug 15 2021, 18:36:22 CEST
   </td>
   <td>
    Wow, you get it!
   </td>
  </tr>
  <tr bgcolor="#D4C0A1">
   <td valign="top" width="25%">
    Aug 01 2021, 21:25:39 CEST
   </td>
   <td>
    Next Time
   </td>
 </tr>
</table>

i got the date with this code:

print(soup.find_all('td', {'valign': 'top'})[0].get_text())

show this

Aug 15 2021, 18:36:22 CEST

but i cant find any solution to get the next td of the date

1 Answers

If html_doc contains the HTML snippet from the question:

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

txt = soup.select_one('td[valign="top"] + td').get_text(strip=True)
print(txt)

Prints:

Wow, you get it!

Or:

txt = soup.find("td", {"valign": "top"}).find_next("td").get_text(strip=True)
Related