欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)自動打開電腦應(yīng)用的示例代碼

 更新時(shí)間:2020年04月17日 10:07:49   作者:!AWESOME!  
這篇文章主要介紹了Python實(shí)現(xiàn)自動打開電腦應(yīng)用的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

由于時(shí)間原因,有時(shí)候可能會錯(cuò)過某個(gè)上網(wǎng)課的時(shí)間段。因此想要實(shí)現(xiàn)自動定時(shí)啟動DingDing。

新手一枚,如有不當(dāng)勿噴望大佬指正。

自動打開DingDing可以由兩種方法實(shí)現(xiàn):

  • 通過找出找出軟件在電腦中快捷方式的位置(電腦屏幕中的坐標(biāo)),使用代碼模擬鼠標(biāo)進(jìn)行雙擊打開。
  • 通過輸入軟件在電腦中的安裝路徑打開軟件。

1.第一種方法:

​在python中,使用pip install pyautogui 安裝第三方庫,在此庫中,可以使用pyautogui.position()獲取當(dāng)前鼠標(biāo)放置位置的坐標(biāo)。我們可以多次使用此方法來實(shí)現(xiàn)獲取任意想要獲取位置的坐標(biāo)。

import pyautogui
import time
#循環(huán)執(zhí)行pyautogui.position()獲取不同位置坐標(biāo)
while True:
 print("當(dāng)前鼠標(biāo)的坐標(biāo)為:"pyautogui.position())
 time.sleep(1)#設(shè)置打印的時(shí)間間隔

多次執(zhí)行結(jié)果:


在使用此方法獲取到想要打開的軟件的快捷方式后,就是進(jìn)行鼠標(biāo)點(diǎn)擊的模擬了

我們可以通過使用pyautogui.click(click=2)實(shí)現(xiàn)雙擊鼠標(biāo)左鍵的效果。

通使用pyautogui.moveTo(x,y)實(shí)現(xiàn)鼠標(biāo)的移動功能。結(jié)合鼠標(biāo)的點(diǎn)擊就可以進(jìn)行自動的打開電腦應(yīng)用的功能了。

import pyautogui
import time
def AutoOpen():
 startPosition = (327,164)#鼠標(biāo)需要移動的位置
 endPosition = (306,216)
 position=[startPosition,endPosition]
 for i in position:
  pyautogui.moveTo(i)#控制鼠標(biāo)移動
  pyautogui.click(clicks=2)#實(shí)現(xiàn)鼠標(biāo)雙擊
  time.sleep(3)
if __name__ == '__main__':
 AutoOpen()

需要注意的是:本方法不能再代碼的編譯器占滿整個(gè)屏幕的時(shí)候使用,那樣獲取的坐標(biāo)位置為編譯器中的位置,位置雖然通用,但是不能實(shí)現(xiàn)點(diǎn)擊應(yīng)用的功能,要點(diǎn)擊的應(yīng)用不能被編譯器所覆蓋。只有這樣才能實(shí)現(xiàn)點(diǎn)擊功能。

2.第二種方法

獲取文件的安裝路徑,找到后綴為.exe的可執(zhí)行的文件,使用os.startfile(Path)打開文件(os庫為自帶庫無需安裝)Path=“F:\XXX\XXX.exe”

import os 
Path = r'F:\DingDing\DingtalkLauncher.exe'
os.startfile(Path)

通過上面三行代碼足以打開需要打開的文件。

import pyautogui
import time
import os
def AutoOpen(Path):
 os.startfile(Path) #os.startfile()打開外部應(yīng)該程序,與windows雙擊相同
 pyautogui.moveTo(306, 216)#pyautogui.moveTo()將鼠標(biāo)移動到指定位置
 time.sleep(6)
 pyautogui.click(clicks=2)#鼠標(biāo)點(diǎn)擊,實(shí)現(xiàn)鼠標(biāo)雙擊
if __name__ == '__main__':
 Path=r'F:\DingDing\DingtalkLauncher.exe'
 AutoOpen()

此方法如果不涉及點(diǎn)擊事件的模擬則沒有要求,如果需要點(diǎn)擊則同上,不能覆蓋住要點(diǎn)擊的位置。

3.定時(shí)打開

在自動打開的功能實(shí)現(xiàn)后,就是簡單的設(shè)置自動打開的時(shí)間了,通過使用time 庫,獲取當(dāng)前時(shí)間。自己可以設(shè)置一個(gè)需要打開的時(shí)間,通過對比當(dāng)前時(shí)間就能實(shí)現(xiàn)定時(shí)自動打開的功能了。

完整代碼:

import pyautogui
import time

def open_app(Path):
 os.startfile(Path) #os.startfile()打開外部應(yīng)該程序,與windows雙擊相同
 pyautogui.moveTo(306, 216)#pyautogui.moveTo()將鼠標(biāo)移動到指定位置
 time.sleep(6)
 pyautogui.click(clicks=2)#鼠標(biāo)點(diǎn)擊,實(shí)現(xiàn)鼠標(biāo)雙擊
def AutoOpen():
 startPosition = (327,164)
 endPosition = (306,216)
 position=[startPosition,endPosition]
 for i in position:
  pyautogui.moveTo(i)
  pyautogui.click(clicks=2)
  time.sleep(3)
if __name__ == '__main__':
 Path=r'F:\DingDing\DingtalkLauncher.exe'
 times = "2020-xx-xx xx:xx"#設(shè)置需要打開的時(shí)間,此時(shí)間看自己需求是否精確到秒("2020-xx-xx xx:xx:xx")
 while True:
  nowtime = time.strftime('%Y-%m-%d %H:%M')
  if (times == nowtime):
   open_app(Path)
   break
  else:
   print(time.strftime('%Y-%m-%d %H:%M:%S'))
   time.sleep(10)

python自動化打開網(wǎng)頁

from selenium.webdriver.firefox.options import Options as FOptions
from selenium.webdriver.chrome.options import Options as Foptions
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains


from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

#firefox設(shè)置代理
profile = FirefoxProfile()
# 激活手動代理配置(對應(yīng)著在 profile(配置文件)中設(shè)置首選項(xiàng))
profile.set_preference("network.proxy.type", 1)
# ip及其端口號配置為 http 協(xié)議代理
profile.set_preference("network.proxy.http", "127.0.0.1")
profile.set_preference("network.proxy.http_port", 8080)

# 所有協(xié)議共用一種 ip 及端口,如果單獨(dú)配置,不必設(shè)置該項(xiàng),因?yàn)槠淠J(rèn)為 False
profile.set_preference("network.proxy.share_proxy_settings", True)

#chrome設(shè)置代理
# options = FOptions()


options = FOptions()
chrome_options = webdriver.FirefoxOptions()
chrome_options.add_argument('--proxy-server=http://127.0.0.1:8080')
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('disable-infobars')
browser = webdriver.Firefox(executable_path="D:/geckodriver.exe",firefox_profile=profile)

browser.maximize_window()
browser.get('https://account.dianping.com/login?redir=http%3A%2F%2Fwww.dianping.com%2F')

button = browser.find_element_by_xpath('/html/body/div/div[2]/div[5]/span')
button.click()

到此這篇關(guān)于Python實(shí)現(xiàn)自動打開電腦應(yīng)用的示例代碼的文章就介紹到這了,更多相關(guān)Python 自動打開電腦應(yīng)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論