您好,我尝试从page_source中提取链接,而我的代码是:
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import html5lib
driver_path = r"C:\Users\666\Desktop\New folder (8)\chromedriver.exe"
driver = webdriver.Chrome(driver_path)
driver.implicitly_wait(10)
driver.get("https://www.milversite.club/milver/outsiders-1x01-video_060893d7a.html")
try:
time.sleep(4)
iframe = driver.find_elements_by_tag_name('iframe')
for i in range(0, len(iframe)):
f = driver.find_elements_by_tag_name('iframe')[i]
driver.switch_to.frame(i)
# your work to extract link
text = driver.find_element_by_tag_name('body').text
print(text)
driver.switch_to.default_content()
output = driver.page_source
print (output)
finally:
driver.quit();
现在我只想抓取此链接
答案 0 :(得分:1)
尝试
element = driver.find_element_by_id('iframevideo')
link = element.get_attribute('src')
答案 1 :(得分:1)
请尝试以下脚本来获取您要解析的链接。您无需切换到iframe即可获取链接。硬编码延迟始终是解析任何动态内容的最糟糕的选择。如果链接在5秒钟后出现,该怎么办。我在下面的脚本中使用了Explicit Wait
,使其更强大。
from selenium import webdriver
from selenium.webdriver.support import ui
driver = webdriver.Chrome()
wait = ui.WebDriverWait(driver, 10)
driver.get("https://www.milversite.club/milver/outsiders-1x01-video_060893d7a.html")
elem = wait.until(lambda driver: driver.find_element_by_id("iframevideo"))
print(elem.get_attribute("src"))
driver.quit()
输出:
https://openload.co/embed/8wVwFQEP1Sw