我正在尝试从一个我喜欢的每日广播节目的在线播放日志中删除艺术家和歌曲的名字。我想最终使用这些收集到的数据,使用python在Spotify或Youtube上编译播放列表。
为什么我的代码不能检索并打印所有歌曲?
import urllib2 from bs4 import BeautifulSoup # latest version bs4 soup = BeautifulSoup(urllib2.urlopen("http://music.cbc.ca/#!/The-Signal").read(), 'lxml') song = soup.find_all("span", {'class': 'logTrackTitle'}) print song
My code
使用Chrome DevTools浏览页面,您会发现页面的“广播日志”部分实际上是一个具有不同URL的iframe。这就是歌曲列表的“来源”。
将iframe的URL交换到代码中会正确返回歌曲。
import urllib2 from bs4 import BeautifulSoup # latest version bs4 soup = BeautifulSoup(urllib2.urlopen("http://music.cbc.ca/The-Signal").read(), 'lxml') song = soup.find_all("span", {'class': 'logTrackTitle'}) print song