Search code examples
pythonweb-scrapingbeautifulsouptags

How to select second element in web scraping with python?


I tried:

    for row in soup.find("tbody").find_all("tr"):
     col = row.find_all("td")
     date =col[0].text
     revenue = col[1].text.replace("$", "").replace(",", "")
    
    tesla_revenue = tesla_revenue.append({"Date":date, "Revenue":revenue}, ignore_index=True)
tesla_revenue.head()

This was the result

Date Revenue
0 2020 31536
1 2019 24578
2 2018 21461
3 2017 11759
4 2016 7000

so the result shows the table on the left, but I want one on the right, what should I do to select the next tbody? (screenshot is in the link)

https://i.sstatic.net/3Lepx.png


Solution

  • If you want to select second table (<tbody> tag), you can do:

    for row in soup.select("tbody")[1].find_all("tr"):  # <-- note the [1] to select second <tbody>
        # ...