gpt4 book ai didi

python-3.x - 抓取youtube播放列表

转载 作者:行者123 更新时间:2023-12-03 06:14:44 26 4
gpt4 key购买 nike

我一直在尝试编写一个python脚本,该脚本将为我获取包含将提供其链接的播放列表中歌曲的名称。例如终端上的https://www.youtube.com/watch?v=foE1mO2yM04&list=RDGMEMYH9CUrFO7CfLJpaD7UR85wVMfoE1mO2yM04

我发现可以使用“li”标签或“h4”标签提取名称。
我写了下面的代码,

  import sys

link = sys.argv[1]


from bs4 import BeautifulSoup
import requests

req = requests.get(link)

try:
req.raise_for_status()
except Exception as exc:
print('There was a problem:',exc)

soup = BeautifulSoup(req.text,"html.parser")

然后我尝试使用li-tag作为:
i=soup.findAll('li')
print(type(i))
for o in i:
print(o.get('data-video-title'))

但是它打印的次数“没有”。我相信它无法访问那些包含data-video-title属性的li标签。

然后我尝试使用div和h4标签,
for i in soup.findAll('div', attrs={'class':'playlist-video-description'}):
o = i.find('h4')
print(o.text)

但是什么也没有发生。

最佳答案

import requests
from bs4 import BeautifulSoup

url = 'https://www.youtube.com/watch?v=foE1mO2yM04&list=RDGMEMYH9CUrFO7CfLJpaD7UR85wVMfoE1mO2yM04'

data = requests.get(url)
data = data.text
soup = BeautifulSoup(data)

h4 = soup.find_all("h4")
for h in h4:
print(h.text)

输出:
      Mike Posner - I Took A Pill In Ibiza (Seeb Remix) (Explicit)


Alan Walker - Faded


Calvin Harris - This Is What You Came For (Official Video) ft. Rihanna


Coldplay - Hymn For The Weekend (Official video)


Jonas Blue - Fast Car ft. Dakota


Calvin Harris & Disciples - How Deep Is Your Love


Galantis - No Money (Official Video)


Kungs vs Cookin’ on 3 Burners - This Girl


Clean Bandit - Rockabye ft. Sean Paul & Anne-Marie [Official Video]


Major Lazer - Light It Up (feat. Nyla & Fuse ODG) [Remix] (Official Lyric Video)


Robin Schulz - Sugar (feat. Francesco Yates) (OFFICIAL MUSIC VIDEO)


DJ Snake - Middle ft. Bipolar Sunshine


Jonas Blue - Perfect Strangers ft. JP Cooper


David Guetta ft. Zara Larsson - This One's For You (Music Video) (UEFA EURO 2016™ Official Song)


DJ Snake - Let Me Love You ft. Justin Bieber


Duke Dumont - Ocean Drive


Galantis - Runaway (U & I) (Official Video)


Sigala - Sweet Lovin' (Official Video) ft. Bryn Christopher


Martin Garrix - Animals (Official Video)


David Guetta & Showtek - Bad ft.Vassy (Lyrics Video)


DVBBS & Borgeous - TSUNAMI (Original Mix)


AronChupa - I'm an Albatraoz | OFFICIAL VIDEO


Lilly Wood & The Prick and Robin Schulz - Prayer In C (Robin Schulz Remix) (Official)


Kygo - Firestone ft. Conrad Sewell


DEAF KEV - Invincible [NCS Release]


Eiffel 65 - Blue (KNY Factory Remix)

关于python-3.x - 抓取youtube播放列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45805409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com