【问题标题】:How to select second element in web scraping with python?如何使用python选择网页抓取中的第二个元素?
【发布时间】:2021-06-18 04:49:49
【问题描述】:

我试过了:

    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()

这就是结果

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

所以结果显示在左边的表格,但我想要一个在右边,我应该怎么做才能选择下一个tbody? (截图在链接中)

https://i.stack.imgur.com/3Lepx.png

【问题讨论】:

    标签: python web-scraping beautifulsoup tags


    【解决方案1】:

    如果你想选择第二个表(<tbody>标签),你可以这样做:

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

    【讨论】:

      猜你喜欢
      • 2019-06-03
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 2022-12-16
      • 1970-01-01
      相关资源
      最近更新 更多