从网页上抓取youtube链接

问题描述 投票:0回答:1

我一直试图从网页上抓取youtube链接,但没有任何效果。 This is a picture of what I've been trying to scrape.

这是我最近尝试过的代码:

youtube_link = soup.find("a", class_="ytp-title-link yt-uix-sessionlink")

这是youtube链接所在网站的链接:https://www.electronic-festivals.com/event/i-am-hardstyle-germany

我真的需要这个工作。提前致谢。

python web-scraping hyperlink youtube screen-scraping
1个回答
0
投票

大多数youtube链接都在iframe内,javascript也需要运行。尝试使用硒。以下提取含有srce的任何hrefyoutub。我只输入托管youtube剪辑的密钥iframe。你可以循环所有iframes检查。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

def addItems(links, final):
    for link in links:
        ref = link.get_attribute('src') if link.get_attribute('src') is not None else link.get_attribute('href')
        final.append(ref)
    return final

url = "https://www.electronic-festivals.com/event/i-am-hardstyle-germany" 
driver = webdriver.Chrome()
driver.get(url)
driver.switch_to.frame(driver.find_element_by_css_selector('.media-youtube-player'))
final = []

try:
    links = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "[href*=youtube] , [src*=youtube]")))
    addItems(links, final)
except:
    pass
finally:
    driver.switch_to.default_content()

links = driver.find_elements_by_css_selector('[href*=youtube] , [src*=youtube]')
addItems(links, final)

for link in set(final):
    print(link)

driver.quit()
© www.soinside.com 2019 - 2024. All rights reserved.