我试图在一个网站上找到一个可下载的视频链接。例如,我使用的URL如下https://www.loc.gov/item/2015669100/
。您可以看到mejs__mediaelement
div标记下有一个m3u8视频链接
然而,我的代码没有打印任何内容。这意味着它找不到网站的视频URL
我的代码在下面
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
with open('pages2crawl.txt', 'r') as inFile:
lines = [line.rstrip() for line in inFile]
for page in lines:
req = Request(page, headers={'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(urlopen(req).read(), 'html.parser')
pages = soup.findAll('div', attrs={'class' : 'mejs__mediaelement'})
for e in pages:
video = e.find("video").get("src")
if video.endswith("m3u8"):
print(video)
如果您只想制作一个简单的脚本,那么使用正则表达式可能会更容易
您可以使用CSS选择器
source[type="application/x-mpegURL"]
提取MPEG链接(或source[type="video/mp4"]
提取mp4链接):印刷品:
相关问题 更多 >
编程相关推荐