python - 为什么它没有从 YouTube 获取任何 'a' 标签?

标签 python beautifulsoup youtube screen-scraping

我正在尝试通过 BeautifulSoup 从给定的输入 channel 链接获取所有视频链接。我发现视频的所有“a”标签的 id 都是“video-title”,但下面的代码没有给出任何输出:

import requests
from bs4 import BeautifulSoup

source = requests.get('https://www.youtube.com/user/TheCraftingLab/featured').text
soup = BeautifulSoup(source, 'html.parser')

container = soup.findAll("a", {id: "video-title"})
for i in container:
    print(i)

怎么了?
enter image description here

最佳答案

您尝试获取的页面可能是使用 JS 呈现的。所以你可以使用requests-html将 JS 作为 Web 驱动程序执行并返回加载页面的全部内容的模块。

from requests_html import HTMLSession
from bs4 import BeautifulSoup

URL = "https://www.example.com"

with HTMLSession() as session:
    response = session.get(URL)
    response.html.render()
    soup = BeautifulSoup(response.html.html, 'html.parser')

for i in soup.findAll("a", {id: "video-title"}):
    print(i)

关于python - 为什么它没有从 YouTube 获取任何 'a' 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61854338/

相关文章:

python - 解析非标准 XML(CDATA 标记)

python - BeautifulSoup 在使用 find_all 时显示 "' NoneType' 对象不可调用”

python - django-channels 是否适合实时游戏?

python - 在 numpy 数组外切片

Python BeautifulSoup 相当于 lxml make_links_absolute

javascript - 如何使用 "Youtube Iframe API "将字幕添加到 YouTube 视频

ios - 在uiwebview中加载youtube视频,并且播放时不会全屏显示

actionscript-3 - 用Flash下载YouTube视频

python - 在 Tensorboard 中组织运行

python - 为什么使用 sys.path.append(path) 而不是 sys.path.insert(1, path)?