<a class='comment_btn j a_show_login' href='https://www.douban.com/register?reason=review' rel='nofollow'>
        <span>我要写短评</span>
    </a>

        
<h2>
    <i class=''>穿靴子的猫2的短评</i>
          · · · · · ·
        <span class='pl'>
        (
            <a>全部 32103 条</a>
        )
        </span>
</h2>

    </div>

我现在写爬虫代码,想定位上面div(id='comments-section')下的span(class='pl')内容:可以使用BeautifulSoup库进行解析和定位,示例代码如下:

from bs4 import BeautifulSoup

# 假设html为所爬取的网页源代码
soup = BeautifulSoup(html, 'html.parser')

# 定位comments-section div
comments_section = soup.find('div', {'id': 'comments-section'})

# 定位span标签
pl_span = comments_section.find('span', {'class': 'pl'})

# 输出全部条目数
print(pl_span.a.string)

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

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