[Python] Requests and BeautifulSoup to get video length from YouTube

Discussão em 'Python' iniciado por Stack, Abril 18, 2025.

  1. Stack

    Stack Membro Participativo

    In getting the video length from a YouTube url, Inspect from web browser shows there's a line:

    [​IMG]

    Then I use requests and BeautifulSoup to get it:

    import requests
    from bs4 import BeautifulSoup

    url = "https://www.youtube.com/watch?v=ANYyoutubeLINK"

    response = requests.get(url)
    response.raise_for_status()

    soup = BeautifulSoup(response.text, 'html.parser')

    duration_span = soup.find_all('span', class_='ytp-time-duration')

    print (duration_span)


    Neither "soup.find_all" nor "soup.find" works. What went wrong?

    Continue reading...

Compartilhe esta Página