【发布时间】:2014-07-14 17:13:42
【问题描述】:
我目前正在 Python 2.7.6 中尝试 Beautiful Soup 4
现在,我有一个简单的脚本来抓取 Soundcloud.com。我正在尝试打印页面上按钮标签的数量,但没有得到预期的答案。
from bs4 import BeautifulSoup
import requests
page = requests.get('http://soundcloud.com/sondersc/waterfalls-sonder')
data = page.text
soup = BeautifulSoup(data)
buttons = soup.findAll('button')
print len(buttons)
当我运行它时,我得到了输出
num buttons = 0
这让我很困惑。我知道按钮标签存在于此页面上,因此它不应该返回 0。在检查波形正下方的按钮元素时,我发现这些...
<button class="sc-button sc-button-like sc-button-medium sc-button-responsive" tabindex="0" title="Like">Like</button>
<button class="sc-button sc-button-medium sc-button-responsive sc-button-addtoset" tabindex="0" title="Add to playlist">Add to playlist</button>
<button class="sc-button sc-button-medium sc-button-responsive sc-button-addtogroup" tabindex="0" title="Add to group">Add to group</button>
<button class="sc-button sc-button-share sc-button-medium sc-button-responsive" title="Share" tabindex="0">Share</button>
起初我认为我试图找到按钮元素的方式不正确。但是,如果我修改我的代码以抓取任意 youtube 页面...
page = requests.get('http://www.youtube.com/watch?v=UiyDmqO59QE')
然后我得到输出
num buttons = 37
这意味着soup.findAll('button') 正在做它应该做的事情,只是不在soundcloud 上。
我也尝试过指定我想要的确切按钮,期望得到 1 的返回结果
buttons = soup.findAll('button', class_='sc-button sc-button-like sc-button-medium sc-button-responsive')
print 'num buttons =', len(buttons)
但它仍然返回 0。
我有点难倒这个。谁能解释这是为什么?
【问题讨论】:
-
“
标签: python button tags beautifulsoup soundcloud