Home > OS >  Is there a way to list all video URLs of YouTube search results in Python?
Is there a way to list all video URLs of YouTube search results in Python?

Time:11-18

I'm using Playwright and BeautifulSoup, I can see important part of the URL (href="/watch?v=5iK4_44i8jU") but have not been able to list it, what am I missing?

# pip install playwright
# playwright install

from playwright.sync_api import sync_playwright
import regex as re
from bs4 import BeautifulSoup

with sync_playwright() as p:
    browser=p.chromium.launch(headless=True)
    page=browser.new_page()
    page.goto('https://www.youtube.com/results?search_query=apple pokemon', wait_until='networkidle')
    html = page.inner_html('#content')
    soup = BeautifulSoup(html, 'html.parser')
    print(soup.find_all("a", {"class":"yt-simple-endpoint style-scope ytd-video-renderer"}))
    browser.close()

CodePudding user response:

I believe you want something like this:

for element in soup.find_all("a", {"class":"..."}):
    print(element['href'])
  • Related