没有用python代码提取的Javascript代码

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

我试图从网站中提取.mp4链接,该链接仅在Web浏览器的“Inspect Element”选项卡中显示。

我在互联网上读到我需要使用selenium,例如PhantomJS来获取该代码。我试了但是我得到的HTML文件在“显示源代码”中可见

from selenium import webdriver

driver = webdriver.PhantomJS(executable_path=r'C:\Users\Nevendary\Desktop\phantomjs-2.1.1-windows\bin\phantomjs')
driver.get("https://filmovitica.com/pucanj-u-sljiviku-preko-reke-1978-domaci-film-gledaj-online/")
driver.implicitly_wait(30)

print(driver.page_source)

我希望得到的代码包括:https://fs40.gounlimited.to/tea5u5akd32qzxfffpqyfndb6resauu5w43w7enoxkvu6sjtrf5hfhbz3ika/v.mp4

但我得到了网站的普通HTML

javascript python html selenium phantomjs
3个回答
0
投票

您可以直接获取视频元素的src属性,而不是搜索页面源,该属性包含您所追求的链接。

视频链接在iframe。在不切换到帧的情况下获取页面源不会返回视频链接。

我用chromedriver作为例子。

试试这个:

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

driver = webdriver.Chrome(executable_path="chromedriver.exe")

wait = WebDriverWait(driver, 20)
driver.get("https://filmovitica.com/pucanj-u-sljiviku-preko-reke-1978-domaci-film-gledaj-online/")

vframe = driver.find_element_by_xpath("//iframe[@width='900']")

driver.switch_to.frame(vframe)

videoElement = wait.until(EC.visibility_of(driver.find_element(By.CSS_SELECTOR, "#vplayer > div > div.container > video")))

print(videoElement.get_attribute('src'))

driver.quit()



0
投票

而不是PhantomJS尝试使用ChromeDriverheadless选项。这给了我你想要的输出。

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
driver=webdriver.Chrome(executable_path='path of chrome driver',options=chrome_options)
driver.get("https://filmovitica.com/pucanj-u-sljiviku-preko-reke-1978-domaci-film-gledaj-online/")
print(driver.page_source)

注意:如果您没有根据浏览器兼容性安装chromedriver,可以从以下链接下载chromedriver。请在下载任何Chrome驱动程序之前阅读发行说明以获得兼容性。 Download Chrome driver

使用Beautiful Soup这是python库的另一种方法。

 import requests
    from bs4 import BeautifulSoup
    data=requests.get('https://filmovitica.com/pucanj-u-sljiviku-preko-reke-1978-domaci-film-gledaj-online/')
    soup=BeautifulSoup(data.text,'html.parser')
    print(soup)

注意:很容易安装pip install beautifulsoup4您可以查看以下链接关于Beautiful Soup Beautiful Soup


0
投票

检查html确实看起来链接是在iframe使用的同一个url中生成的。您可以使用请求来获取:

import requests
from bs4 import BeautifulSoup
res = requests.get('https://filmovitica.com/pucanj-u-sljiviku-preko-reke-1978-domaci-film-gledaj-online/')
soup = bs(res.content, 'lxml')
print(soup.select_one('iframe[allowfullscreen]')['src'])

您可以找到在uri中的一个脚本标记中生成的字符串(您的字符串)(请参阅以蓝色开头突出显示的行:

enter image description here

后来在那个js:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.