可以使用CSS选择器中的伪类nth-of-type来选中第二个a标签,代码如下:

from bs4 import BeautifulSoup

html = """
<div class="boxbg_7ree">
    <div class="threadline_7ree">
        <a href="#">第一个a标签</a>
    </div>
    <div class="threadline_7ree">
        <a href="#">第二个a标签</a>
    </div>
</div>
"""

soup = BeautifulSoup(html, "html.parser")
second_a = soup.select(".boxbg_7ree .threadline_7ree a:nth-of-type(2)")
print(second_a[0].string)

输出结果为:第二个a标签

在python的BeautifulSoup模块中使用select方法第一个参数填写“.boxbg_7ree .threadline_7ree a”会选出来俩个a标签的内容,如何只选中第二个

原文地址: https://www.cveoy.top/t/topic/vPO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录