如何在 python 中使用 "control + S" 保存网页?



我正在使用以下代码,但找不到文件"test.html";在指定路径上的本地计算机上。此外,当我运行它时,代码不会出错。有人能帮我定位我的文件吗?

pyautogui.hotkey('ctrl', 's')
time.sleep(2)
FILE_NAME = 'C:\file_path_on_my_computer\test.html'
pyautogui.typewrite(FILE_NAME)
pyautogui.hotkey('enter')

GitHub上的这个问题中,我可以看到这对许多使用特定操作系统和Python版本的人来说是不起作用的。您可以使用KeyDownKeyUppress

pyautogui.keyDown('ctrl') # hold ctrl key
pyautogui.press('s') # press s key
pyautogui.keyUp('ctrl') # release ctrl key
time.sleep(2)
FILE_NAME = 'C:\file_path_on_my_computer\test.html'
pyautogui.typewrite(FILE_NAME)

hotkey过去被传递几个键串,这些键串将按顺序按下,然后按相反的顺序释放。不要用它只按回车键,而是用press方法代替

pyautogui.press('enter')

最新更新