我正在尝试使用Python和BeautifulSoup模块从Acharts.co中获取前100个歌曲排行榜。到目前为止,我已经设法获得了图表中某个特定位置的歌曲标题,但我有点难以获得艺术家的名字
import requests
from bs4 import BeautifulSoup
url = "https://acharts.co/canada_singles_top_100/2021/05"
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en,de;q=0.9,en-US;q=0.8,fr-FR;q=0.7,fr;q=0.6,es;q=0.5",
"authority": "acharts.co",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 YaBrowser/17.6.1.749 Yowser/2.5 Safari/537.36"
}
response = requests.get(url, headers=headers)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text, 'html.parser')
for item in soup.select("td"):
if item['class'][0] == 'cPrinciple':
song = item.a.span.get_text()
print(song)
下面是我试图解析的HTML部分:
<td class="cPrinciple" itemprop="item" itemscope itemtype="http://schema.org/MusicRecording">
<a href="https://acharts.co/song/156580" itemprop="url"><span itemprop="name">Mood</span></a>
<br />
<span class="Sub">
<span itemprop="byArtist" itemscope itemtype="http://schema.org/MusicGroup">
<meta itemprop="url" content="https://acharts.co/artist/24kgoldn" />
<span itemprop="name">24Kgoldn</span>
</span> and
<span itemprop="byArtist" itemscope itemtype="http://schema.org/MusicGroup">
<meta itemprop="url" content="https://acharts.co/artist/iann_dior" />
<span itemprop="name">Iann Dior</span>
</span>
</span>
那么在上面的片段中,我将如何提取“情绪”(歌曲标题)、“24kGldn”(艺术家1)和“Iann Dior”(艺术家2)? 提前谢谢
更简洁的方式(使用列表理解):
你可以这样做:
相关问题 更多 >
编程相关推荐