Skip to main content
 首页 » 编程设计

python之如何使用 BeautifulSoup 找到特定的视频 html 标签

2025年05月04日14pander-it

有谁知道如何在 python 中使用 beautifulsoup。

我有一个包含不同 URL 列表的搜索引擎。

我只想获取包含视频嵌入 url 的 html 标记。并获取链接。

例子

import BeautifulSoup 
 
html = '''https://archive.org/details/20070519_detroit2''' 
    #or this.. html = '''http://www.kumby.com/avatar-the-last-airbender-book-3-chapter-5/''' 
    #or this... html = '''https://www.youtube.com/watch?v=fI3zBtE_S_k''' 
 
soup = BeautifulSoup.BeautifulSoup(html) 

接下来我该做什么。获取视频的 html 标签,或对象或视频的确切链接..

我需要将它放在我的 iframe 上。我会将 python 集成到我的 php 中。所以获取视频的链接并使用 python 输出它,然后我将在我的 iframe 上回显它。

请您参考如下方法:

你需要获取页面的html而不仅仅是url

像这样使用内置库 urllib:

import urllib 
from bs4 import BeautifulSoup as BS 
 
url = '''https://archive.org/details/20070519_detroit2''' 
#open and read page 
page = urllib.urlopen(url) 
html = page.read() 
#create BeautifulSoup parse-able "soup" 
soup = BS(html) 
#get the src attribute from the video tag 
video = soup.find("video").get("src") 

我还注意到,对于您正在使用的网站,要获取嵌入链接,只需将链接中的 details 更改为 embed,这样它看起来像这样:

https://archive.org/embed/20070519_detroit2 

因此,如果您想对多个 url 执行此操作而无需解析,只需执行以下操作:

url = '''https://archive.org/details/20070519_detroit2''' 
spl = url.split('/') 
spl[3] = 'embed' 
embed = "/".join(spl) 
print embed 

编辑

要获取您在编辑中提供的其他链接的嵌入链接,您需要查看您正在解析的页面的 html,查找直到找到链接,然后获取其标签和属性

对于

'''http://www.kumby.com/avatar-the-last-airbender-book-3-chapter-5/''' 

就这样

soup.find("iframe").get("src") 

iframe 因为链接在 iframe 标签中,.get("src") 因为链接是 src 属性

你可以尝试下一个,因为如果你想在未来能够做到这一点,你应该学习如何去做:)

祝你好运!