Most of the youtube links are within an iframe
and javascript also needs to run. Try using selenium. The following extracts any src
or href
containing youtub
e. I only enter the key iframe hosting the youtube clip. You could loop all iframes
checking.
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()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…