刮刮youtube播放列表

时间:2017-08-21 20:58:12

标签: python-3.x web-scraping youtube beautifulsoup

我一直在尝试编写一个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'))

但它打印了#34;无"那些时间。我相信它无法达到那些包含data-video-title属性的li标签。
然后我尝试使用div和h4标签,

for i in soup.findAll('div', attrs={'class':'playlist-video-description'}):
    o = i.find('h4')
    print(o.text)

但没有再发生......

2 个答案:

答案 0 :(得分:0)

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)

答案 1 :(得分:0)

好的,伙计们,我已经知道发生了什么。我的代码是完美的,它工作正常,问题是我从终端传递链接作为参数,并且同时,链接包含一些符号,这些符号以其他方式解释为例如。 ('&安培;&#39)。 现在我将链接作为字符串传递到终端,一切正常。如此愚蠢又耗时的错误。