如何使用 python 在后台打开网站然后关闭而不中断程序流?
How to open websites in background and then close without interrupting flow of program using python?
我想使用 python 在后台打开特定视频。为此,我完成了以下编码
chrome= "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
webbrowser.get(chrome).open_new("https://www.youtube.com/watch?v=AFNUeUed8Ro&ab_channel=NDTV")
time.sleep(4)
os.system("taskkill /im chrome.exe /f")**
但是在打开 URL 之后 程序不会继续 直到我手动关闭该站点。
如果我事先打开 chrome 浏览器,一切正常。但这不是我想要的。
我想在后台打开它,因为我只需要音频,因为我的项目是为盲人娱乐的,它使用手势打开网站,然后使用相同的手势关闭。
我建议您使用 Selenium 而不是 Webbrowser 包,因为它具有更多功能。
使用
安装 Selenium 包
pip install selenium
从
下载Chrome与您的Chrome版本匹配的网络驱动程序
https://sites.google.com/a/chromium.org/chromedriver/downloads
将驱动程序 .exe 文件放在 python 脚本所在的位置
试试这个脚本
import selenium
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 3)
visible = EC.visibility_of_element_located
browser.get('https://www.youtube.com/watch?v=AFNUeUed8Ro&ab_channel=NDTV')
wait.until(visible((By.ID, "video-title")))
browser.find_element_by_id("video-title").click()
browser.minimize_window()
time.sleep(7)
browser.quit()
不要在 Opencv 捕获循环中使用 time.sleep(),因为它会阻止捕获。如果您想使用时间延迟,请考虑使用单独的线程。
看看
https://www.tutorialspoint.com/python/python_multithreading.htm
我想使用 python 在后台打开特定视频。为此,我完成了以下编码
chrome= "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
webbrowser.get(chrome).open_new("https://www.youtube.com/watch?v=AFNUeUed8Ro&ab_channel=NDTV")
time.sleep(4)
os.system("taskkill /im chrome.exe /f")**
但是在打开 URL 之后 程序不会继续 直到我手动关闭该站点。 如果我事先打开 chrome 浏览器,一切正常。但这不是我想要的。
我想在后台打开它,因为我只需要音频,因为我的项目是为盲人娱乐的,它使用手势打开网站,然后使用相同的手势关闭。
我建议您使用 Selenium 而不是 Webbrowser 包,因为它具有更多功能。
使用
安装 Selenium 包pip install selenium
从
下载Chrome与您的Chrome版本匹配的网络驱动程序https://sites.google.com/a/chromium.org/chromedriver/downloads
将驱动程序 .exe 文件放在 python 脚本所在的位置
试试这个脚本
import selenium
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 3)
visible = EC.visibility_of_element_located
browser.get('https://www.youtube.com/watch?v=AFNUeUed8Ro&ab_channel=NDTV')
wait.until(visible((By.ID, "video-title")))
browser.find_element_by_id("video-title").click()
browser.minimize_window()
time.sleep(7)
browser.quit()
不要在 Opencv 捕获循环中使用 time.sleep(),因为它会阻止捕获。如果您想使用时间延迟,请考虑使用单独的线程。 看看
https://www.tutorialspoint.com/python/python_multithreading.htm