【发布时间】:2021-05-06 06:57:27
【问题描述】:
我有数以万计的网址,我想将它们的网页保存到我的计算机上。
我正在尝试使用 pywinauto 自动使用的 Chrome 打开和保存这些网页。 我可以使用以下代码打开网页:
from pywinauto.application import Application
import pyautogui
chrome_dir = 'C:\Program Files\Google\Chrome\Application\chrome.exe'
start_args = ' --force-renderer-accessibility --start-maximized https://pythonexamples.org/'
app = Application(backend="uia").start(chrome_dir+start_args)
我想进一步向网页发送快捷方式以将其保存为 mhtml。 Ctrl+Shift+Y 是将网页保存为 mhmtl 的 Chrome 扩展程序(称为 SingleFile)的快捷方式。然后我想通过键入“Ctrl + F4”关闭选项卡,然后再打开另一个选项卡并重复相同的过程。
密钥未成功发送到 Chrome。
# Sent shortcut (Ctrl+Shift+Y)
pyautogui.press(['ctrl', 'shift', 'y'])
# Close the current tab:
pyautogui.press(['ctrl', 'f4'])
我被困在这一步。这样做的正确方法是什么?谢谢! 尝试了 Selenium 等其他替代方案,但被远程服务器阻止。
【问题讨论】:
-
你可以用 Pywinauto 做到这一点,但这不是最好的方法。你应该看看:pyppeteer.github.io/pyppeteer 和 stackoverflow.com/questions/54814323/…
-
我试过 Selenium。挑战在于远程服务器很快检测到我正在使用抓取工具并阻止了我。不确定 puppeteer 是否有同样的问题。
-
SingleFile 可以从 CLI (github.com/gildas-lormeau/SingleFile/tree/master/cli) 运行并抓取网站。
-
这很棒。非常感谢。
标签: python python-3.x pyautogui pywinauto