使用Selenium和xpath查找网站(如Fmovies)上MP4视频的源链接

2024-04-24 04:50:52 发布

您现在位置:Python中文网/ 问答频道 /正文

selenium和xpath。我有一个python脚本,它使用selenium从电影网站上获取视频源。我可以使用Selenium获得播放视频的脚本,但我想将src链接刮到MP4视频文件。我认为我的xpath语法不正确

**** Code ******
# Load selenium components
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time

browser = webdriver.Chrome(executable_path=r"C:\\temp\\chromedriver.exe")

## Link to the movie as an example
url = "https://vw.ffmovies.sc/film/fatman-2020/watching/?server_id=3"
browser.get(url)
element = WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='player']")))
clickable = browser.find_element_by_id("player")
clickable.find_element_by_xpath('.//*').click()
browser.switch_to.frame("iframe-embed") 
time.sleep(5)

### This is where I am stuck.. It cannot find the xpath element.... 
##########################################################################################
## I am getting the xpath wrong. I want the video link to be stored in the link variable. 
link=browser.switch_to.frame(browser.find_element_by_xpath('//*[@id="player"]/iframe').get_attribute('src')) 
## Getting error in the above code                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

browser.close()

任何建议都将不胜感激。谢谢


Tags: thetofromimportbrowseridbyselenium
1条回答
网友
1楼 · 发布于 2024-04-24 04:50:52

id='player' 在iframe之外,因此不应在xpath中使用它

enter image description here

所以你应该把iFrAMP视为新上下文的根。

请尝试以下操作,而不是browser.find_element_by_xpath('//*[@id="player"]/iframe').get_attribute('src')

browser.find_element_by_xpath('.//video/source').get_attribute('src')

相关问题 更多 >