0

I have some HTML code in a table like this:

<html>
  <head></head>
  <body>
    <tr>
      <td class="class1">data1<td/>
      <td class="class2">data2<td/>
      <td class="class3">data3<td/>
      <td class="class4">data4<td/>
      <td class="class5">data5<td/>
    </tr>
  </body>
</html>

How do I use beautiful soup to get the data/string with a specific class? When I use print(soup.body.td) I just get the first result.

gman1230321
  • 105
  • 1
  • 2
  • 10
  • 1
    Possible duplicate of [How to find elements by class](https://stackoverflow.com/questions/5041008/how-to-find-elements-by-class) – DeepSpace Sep 21 '18 at 14:43

1 Answers1

0

Rextester

Try this:

from bs4 import BeautifulSoup
html = '''<html>
  <head></head>
  <body>
    <tr>
      <td class="class1">data1<td/>
      <td class="class2">data2<td/>
      <td class="class3">data3<td/>
      <td class="class4">data4<td/>
      <td class="class5">data5<td/>
    </tr>
  </body>
</html>'''

soup = BeautifulSoup(html, 'lxml')
results = soup.find("td", {"class" : "class1"})
print(results.text)
Ian-Fogelman
  • 1,595
  • 1
  • 9
  • 15