【问题标题】:discord.py BS4 selecting second div with same class namediscord.py BS4 选择具有相同类名的第二个 div
【发布时间】:2021-01-02 15:16:18
【问题描述】:

我有以下(简化的)html 代码:

<div class="container">
*other html*
</div>
<div class="container">
*html i need*
</div>

如何获取第二个 div 的信息?

【问题讨论】:

  • soup.find_all('div', class_='container')[1] ?
  • @AndrejKesely 我将其添加到我的答案中

标签: python html beautifulsoup discord discord.py


【解决方案1】:

使用 BeautifulSoup 的 find_all() 函数,您可以在 HTML 中获得符合您的条件的元素列表。见:https://www.crummy.com/software/BeautifulSoup/bs4/doc/#calling-a-tag-is-like-calling-find-all

所以你可以通过调用找到第二个元素:

soup.find_all('div', class_='container')[1]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 2022-10-31
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    相关资源
    最近更新 更多