首页 > 解决方案 > 尝试用beautifulsoup 抓取soundcloud

问题描述

我正在尝试抓取 soundcloud和其他音乐平台的数据,但我似乎被困在 soundcloud,因为我得到 None、AttributeError 或 [],但是当我尝试抓取常规网站(非音乐)时。我得到数据。我在做什么错请帮忙。

from bs4 import BeautifulSoup
import requests

html_text = requests.get('https://soundcloud.com/jujubucks').text
soup = BeautifulSoup(html_text,'lxml')
song = soup.find('li', class_='soundList__item')
print(song)

此代码返回此。

None or AttributeError.

标签: pythonhtmlweb-scrapingbeautifulsoupdata-mining

解决方案


查看原始输出(代码中的变量汤)。

此代码提取原始歌曲标题:

from bs4 import BeautifulSoup
import requests

html_text = requests.get('https://soundcloud.com/jujubucks').text
soup = BeautifulSoup(html_text, 'lxml')
song = soup.find_all('h2', itemprop='name')
print(song)

上面代码的输出列表中的项目示例:

<h2 itemprop="name"><a href="/jujubucks/squad-too-deep-ft-cool-prince" itemprop="url">Squad Too Deep Ft. Cool Prince (Outro)</a>

但如果没有 selenium 或 scrapy,您无法从该网站上抓取所有数据,它们使用动态加载的内容。


推荐阅读