-1

So I have something like this

<tr>
  <td class="name">
     qwer
  </td>
  <td class="value">
     rtyu
  </td>
</tr>

<tr>
  <td class="name">
     asdf
  </td>
  <td class="value">
     zxcv
  </td>
</tr>

Looking for a method to get value element text (zxcv) if name element contains an entry of text (asdf)

dejanualex
  • 3,872
  • 6
  • 22
  • 37
Hotkuk
  • 106
  • 9

1 Answers1

0

You can you beautifulsoup, and create a list of all <tr> then iterate over each one and check if the text in <td class="name"> is asdf, if true we then get the text inside <td class="value">.

In code:

import bs4 as bs

html= """<tr>
  <td class="name">
     qwer
  </td>
  <td class="value">
     rtyu
  </td>
</tr>

<tr>
  <td class="name">
     asdf
  </td>
  <td class="value">
     zxcv
  </td>
</tr>"""

soup = bs.BeautifulSoup(html,'lxml')
trs = soup.findAll("tr")

for tr in trs:
    td_name=tr.find("td", {"class": "name"})
    if(td_name.text.strip() == "asdf"):
        td_value=tr.find("td", {"class": "value"})
        print(td_value.text.strip())
Nidal Barada
  • 373
  • 2
  • 14