【问题标题】:How to navigate through elements using find or find_all instead of select?如何使用 find 或 find_all 而不是 select 浏览元素?
【发布时间】:2022-01-08 12:43:46
【问题描述】:

据我所知,您可以使用 select 方法浏览页面元素,例如:

text = soup.select('.css-lw5dil > .css-1p6sxhz')

find 方法有什么办法吗?

【问题讨论】:

    标签: python python-3.x beautifulsoup


    【解决方案1】:

    select_one 等于 find 并且 select 等于 find_all or findAll select_one /find 用于单个元素,select/find_all 用于list 是的,您可以导航,但在这种情况下,css 选择器的定位器更加灵活。

    text = soup.select('.css-lw5dil > .css-1p6sxhz')
    

    使用汤定位器如下:

    text = soup.find('tag name',class_="css-lw5dil").find_all('tag name',class_= "css-1p6sxhz")
    

    现在您可以像 css 选择器一样对其进行迭代,如下所示:

    for i in text:
        do something new
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      相关资源
      最近更新 更多