小编mor*_*orn的帖子

使用Python请求和Beautiful Soup从span标记中检索数字

我是python和html的新手.我试图使用请求和BeautifulSoup从页面中检索注释的数量.

在此示例中,我尝试获取数字226.以下是我在Chrome中检查页面时可以看到的代码:

<a title="Go to the comments page" class="article__comments-counts" href="http://www.theglobeandmail.com/opinion/will-kevin-oleary-be-stopped/article33519766/comments/">
    <span class="civil-comment-count" data-site-id="globeandmail" data-id="33519766" data-language="en">
    226
    </span>
    Comments
</a>
Run Code Online (Sandbox Code Playgroud)

当我从URL请求文本时,我可以找到代码但是span标记之间没有内容,没有226.这是我的代码:

import requests, bs4

url = 'http://www.theglobeandmail.com/opinion/will-kevin-oleary-be-stopped/article33519766/'
r = requests.get()
soup = bs4.BeautifulSoup(r.text, 'html.parser')

span = soup.find('span', class_='civil-comment-count')
Run Code Online (Sandbox Code Playgroud)

它返回此,与上面相同,但没有226.

<span class="civil-comment-count" data-id="33519766" data-language="en" data-site-id="globeandmail">
</span>
Run Code Online (Sandbox Code Playgroud)

我不知道为什么价值没有出现.提前感谢您的任何帮助.

python beautifulsoup

6
推荐指数
1
解决办法
505
查看次数

标签 统计

beautifulsoup ×1

python ×1