首页 > 解决方案 > 如何用 beautifulsoup 和 selenium 获得这个数字?

问题描述

嘿,我需要这个号码:
27

<a class="r8ZrO" href="/p/B99QGseomNG/comments/">
"View all "
<span>27</span>
" comments"
我试过了:

    driver.get(each)
soup = BeautifulSoup(driver.page_source, "html.parser")
ncomments = soup.find('a', {'class': 'r8ZrO'}).text

当然这不起作用,因为该数字不在给定的班级中。
那么如何获取类里面的内容呢?

这是我的输出:

<a class="r8ZrO" href="/p/B99Ql4cl6MO/comments/">View all <span>4</span> comments</a>

标签: pythonseleniumbeautifulsoup

解决方案


从 bs4 导入 BeautifulSoup

html="""<a class="r8ZrO" href="/p/B99QGseomNG/comments/">
"View all "
<span>27</span>
" comments"""

汤 = BeautifulSoup(html,'lxml')

ncomments = soup.find('a', class_='r8ZrO').span.text


推荐阅读