Skip to main content
 首页 » 编程设计

python之解析 Youtube 播放列表的 HTML

2025年05月04日11JustinYoung

我在解析 YouTube 播放列表的 HTML 时遇到问题。例如,当我检查“https://www.youtube.com/playlist?list=PLS1QulWo1RIYmaxcEqw5JhK3b-6rgdWO_”的标签时。我看到类名“yt-simple-endpoint.style-scope.ytd-playlist-video-renderer”。但是当我使用 bs4 选择元素时,这不起作用。但是,我在网上找到了另一段工作代码,它选择了以下类“pl-video-title-link”。但是我在网页上找不到这个类,而且没有一个标签有这个类?附件是工作代码。任何帮助将不胜感激。

from bs4 import BeautifulSoup as bs 
import requests 
r = requests.get('https://www.youtube.com/playlist?  
list=PLS1QulWo1RIYmaxcEqw5JhK3b-6rgdWO_') 
page = r.text 
soup = bs(page,'html.parser') 
res = soup.find_all('a',{'class':'pl-video-title-link'}) 
for l in res: 
print (l.get("href")) 

请您参考如下方法:

此页面使用 JavaScript 更改其结构,但您可以在下载后打印 soup 并查看视频链接的初始位置。在这种情况下,在标记 <tr> 中与类 pl-video :

from bs4 import BeautifulSoup 
import requests 
 
url = 'https://www.youtube.com/playlist?list=PLS1QulWo1RIYmaxcEqw5JhK3b-6rgdWO_' 
page = requests.get(url) 
soup = BeautifulSoup(page.text, 'lxml') 
 
for i, tr in enumerate(soup.select('tr.pl-video')): 
    print('{}. {}'.format(i + 1, tr['data-title'])) 
    print('https://www.youtube.com' + tr.a['href']) 
    print('-' * 80) 

打印:

1. Shell Scripting Tutorial for Beginners 1 -  Introduction 
https://www.youtube.com/watch?v=cQepf9fY6cE&list=PLS1QulWo1RIYmaxcEqw5JhK3b-6rgdWO_&index=2&t=0s 
-------------------------------------------------------------------------------- 
2. Shell Scripting Tutorial for Beginners 2 - using Variables and Comments 
https://www.youtube.com/watch?v=vQv4W-JfrmQ&list=PLS1QulWo1RIYmaxcEqw5JhK3b-6rgdWO_&index=3&t=0s 
-------------------------------------------------------------------------------- 
3. Shell Scripting Tutorial for Beginners 3 - Read User Input 
https://www.youtube.com/watch?v=AcSkkNAsGCY&list=PLS1QulWo1RIYmaxcEqw5JhK3b-6rgdWO_&index=4&t=0s 
-------------------------------------------------------------------------------- 
 
... all the way to: 
 
32. How Install VirtualBox Guest Additions on Ubuntu 18.04 Guest / virtual machine 
https://www.youtube.com/watch?v=qNecdUsuTPw&list=PLS1QulWo1RIYmaxcEqw5JhK3b-6rgdWO_&index=33&t=0s 
-------------------------------------------------------------------------------- 
33. How to install Java JDK 10 on Ubuntu 18.04 LTS (Debian Linux) 
https://www.youtube.com/watch?v=4RJ60fqeTN4&list=PLS1QulWo1RIYmaxcEqw5JhK3b-6rgdWO_&index=34&t=0s 
--------------------------------------------------------------------------------