【发布时间】:2020-09-30 19:13:42
【问题描述】:
我正在尝试在网站中查找可下载的视频链接。例如,我正在使用像 https://www.loc.gov/item/2015669100/ 这样的网址。可以看到mejs__mediaelementdiv标签下有一个m3u8视频链接。
但是我的代码没有打印任何东西。这意味着它没有找到该网站的视频网址。
我的代码在下面
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)
【问题讨论】:
-
您的问题是,您的 serached div 标签将不会交付(给我)。我可以看到一个 m3u8 文件,但没有一个父母是这个给定类的 div(在浏览器中我看到这个 div)。由于用户代理,您可能会获得浏览器以外的其他数据。您必须查看答案才能找到另一种定位方式,或尝试获取与浏览器相同的数据
标签: python python-3.x beautifulsoup