How to extract text from table moving between<tr> tags using Beautifulsoup

Viewed 44

I need to extract text from a table using BeautifulSoup.

Below is the code which I have written and output

HTML:

<div class="Tech">
  <div class="select">
    <span>Selection is mandatory</span>
  </div>
  <table id="product">
    <tbody>
      <tr class="feature">
        <td class="title" rowspan="3">
          <h2>Information</h2>
        </td>
        <td class="label">
          <h3>Design</h3>
        </td>
        <td class="checkbox">product</td>
      </tr>
      <tr>
        <td class="label">
          <h3>Marque</h3>
        </td>
        <td class="checkbox">
          <input type="checkbox">
          <label>retro</label>
          <a href="link">
          Landlord
         </a>
        </td>
      </tr>
      <tr>
        <td class="label">
          <h3>Model</h3>
        </td>
        <td class="checkbox">model123</td>
      </tr>
import requests
from bs4 import BeautifulSoup

url='someurl.com'
source2= requests.get(url,timeout=30).text
soup2=BeautifulSoup(source2,'lxml')
element2= soup2.find('div',class_='Tech')
pin= element2.find('table',id='product').tbody.tr.text
print(pin)

Output that I am getting is:

Information
Design
product

How to do I move between <tr>s? I need the output as: model123.

1 Answers

To get output model123, you can try:

# search <h3> that contains "Model"
h3 = soup.select_one('h3:contains("Model")')

# search next <td>
model = h3.find_next("td").text
print(model)

Prints:

model123

Or without CSS selectors:

model = (
    soup.find(lambda tag: tag.name == "h3" and tag.text.strip() == "Model")
    .find_next("td")
    .text
)
print(model)
Related