【问题标题】:Why its not fetching any 'a' tags from YouTube?为什么它不从 YouTube 获取任何“a”标签?
【发布时间】:2020-09-03 08:25:32
【问题描述】:

我正在尝试通过 BeautifulSoup 从给定的输入通道链接获取所有视频链接。我发现视频的所有“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填充的
  • @rdas 那我该如何解决呢?
  • 使用 selenium 或 headless chrome 之类的东西。

标签: python beautifulsoup youtube screen-scraping


【解决方案1】:

它通过 JS 加载,如果需要,可以使用 this 模块,它允许在没有浏览器的情况下使用 JS 渲染

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')

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

【讨论】:

    猜你喜欢
    • 2017-06-08
    • 2019-11-15
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多