1

I wath to extract the information about the year, kms and color from this part of the html code using python's BeautifulSoup. Can someone help me out?

                    <h3 class="topNavTitle" id="techParams">Tech parms</h3>
                    <div class="techParamsRow general">
                        <div class="col col12M pr20">
                            <table class="transparentTable">
                                <tr>
                                    <th>Year</th><td>2014</td>
                                </tr>
                                <tr>
                                    <th>Kms</th><td>103 472 km</td>
                                </tr>
                                <tr>
                                    <th>Color</th><td>white</td>
                                </tr>
                            </table>
                        </div>

I tried:

res = requests.get(website)
soup = BeautifulSoup(res.content, "html.parser")
results = soup.find('div', {'class': 'techParamsRow general'})
print(results)

But it's not finding anything. Thank you!

  • What does *not finding anything* means - Your selection is not specific to get the table, but it will find your div. Always look in your soup first - therein lies the truth. `print(soup)` -> Do you find the html in your soup? Update your question with the information and also provide your expected result. Thanks – HedgeHog Jan 03 '22 at 12:44

2 Answers2

1

If I understand you correctly, you can do what you what by using css selectors:

data = soup.select('table.transparentTable tr')
for d in data:
  row = d.select('th,td')
  print(row[0].text,":",row[1].text)

Output:

Year : 2014
Kms : 103 472 km
Color : white
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45