我正在尝试通过 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)
怎么了?
最佳答案
您尝试获取的页面可能是使用 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/