运行 无头 Chrome 来自 python
Running headless Chrome from python
我正在尝试使用 chrome headless 打印成 pdf。我正在使用 windows 10 命令提示符。如果我执行以下操作,一切都会按预期进行:
cd "C:\Program Files (x86)\Google\Chrome\Application"
chrome --headless --print-to-pdf=c:\Users\timmc\Documents\a.pdf --disable-gpu https://www.google.com/
但是,最终我想 运行 使用 subprocess.call() 从 python 脚本执行此命令,而 'Program Files (x86)' 中的空格似乎会造成麻烦。我查看了有关堆栈溢出的其他答案,但没有找到任何有效的方法。如果我尝试以下操作:
C:\"Program Files (x86)"\Google\Chrome\Application\chrome --headless --print-to-pdf=c:\Users\timmc\Documents\b.pdf --disable-gpu https://www.google.com/
我遇到一些非常不寻常的行为,其中 google 打开但不是无头模式,它打开两个选项卡,一个带有 google,另一个试图打开类似程序%20-- original-process-start-time%3D13156438844432514%20--fast-start%20files%20%28x86%29.
任何人都可以解释上述行为吗?
有没有一种简单的方法来处理空格,可以在使用 subprocess.call() 的 python 脚本中工作?
是否有其他方法可以实现相同的目的? (我宁愿不用selenium,直接用chrome headless)
编辑:
我最终希望从我的 python 脚本中 运行 的代码是:
subprocess.call('C:\"Program Files (x86)"\Google\Chrome\Application\chrome --headless --print-to-pdf=c:\Users\timmc\Documents\b.pdf --disable-gpu https://www.google.com/',shell=True)
我不会 运行 Windows,所以我无法为您做任何测试,但我可能可以为您指明正确的方向。
首先,更好的做法是使用 call
的参数列表而不是大字符串。例如 subprocess.call(["echo", "one", "two"])
而不是 subprocess.call("echo", "one two")
。通过这样做,您不必担心引用太多,因为 .call
的每个参数都应该被解释为 chrome
的单个参数,即使它们包含空格。
另外,文档底部有一些关于命令行字符串解释在 Windows 中发生的信息:subprocess.call(["ls", "-l"])
17.1.5.1. Converting an argument sequence to a string on Windows
On Windows, an args sequence is converted to a string that can be parsed using the following rules (which correspond to the rules used by the MS C runtime):
Arguments are delimited by white space, which is either a space or a tab.
A string surrounded by double quotation marks is interpreted as a single argument, regardless of white space contained within. A quoted string can be embedded in an argument.
A double quotation mark preceded by a backslash is interpreted as a literal double quotation mark.
Backslashes are interpreted literally, unless they immediately precede a double quotation mark.
If backslashes immediately precede a double quotation mark, every pair of backslashes is interpreted as a literal backslash. If the number of backslashes is odd, the last backslash escapes the next double quotation mark as described in rule 3.
https://docs.python.org/2/library/subprocess.html#converting-argument-sequence
我正在尝试使用 chrome headless 打印成 pdf。我正在使用 windows 10 命令提示符。如果我执行以下操作,一切都会按预期进行:
cd "C:\Program Files (x86)\Google\Chrome\Application"
chrome --headless --print-to-pdf=c:\Users\timmc\Documents\a.pdf --disable-gpu https://www.google.com/
但是,最终我想 运行 使用 subprocess.call() 从 python 脚本执行此命令,而 'Program Files (x86)' 中的空格似乎会造成麻烦。我查看了有关堆栈溢出的其他答案,但没有找到任何有效的方法。如果我尝试以下操作:
C:\"Program Files (x86)"\Google\Chrome\Application\chrome --headless --print-to-pdf=c:\Users\timmc\Documents\b.pdf --disable-gpu https://www.google.com/
我遇到一些非常不寻常的行为,其中 google 打开但不是无头模式,它打开两个选项卡,一个带有 google,另一个试图打开类似程序%20-- original-process-start-time%3D13156438844432514%20--fast-start%20files%20%28x86%29.
任何人都可以解释上述行为吗?
有没有一种简单的方法来处理空格,可以在使用 subprocess.call() 的 python 脚本中工作?
是否有其他方法可以实现相同的目的? (我宁愿不用selenium,直接用chrome headless)
编辑: 我最终希望从我的 python 脚本中 运行 的代码是:
subprocess.call('C:\"Program Files (x86)"\Google\Chrome\Application\chrome --headless --print-to-pdf=c:\Users\timmc\Documents\b.pdf --disable-gpu https://www.google.com/',shell=True)
我不会 运行 Windows,所以我无法为您做任何测试,但我可能可以为您指明正确的方向。
首先,更好的做法是使用 call
的参数列表而不是大字符串。例如 subprocess.call(["echo", "one", "two"])
而不是 subprocess.call("echo", "one two")
。通过这样做,您不必担心引用太多,因为 .call
的每个参数都应该被解释为 chrome
的单个参数,即使它们包含空格。
另外,文档底部有一些关于命令行字符串解释在 Windows 中发生的信息:subprocess.call(["ls", "-l"])
17.1.5.1. Converting an argument sequence to a string on Windows On Windows, an args sequence is converted to a string that can be parsed using the following rules (which correspond to the rules used by the MS C runtime):
Arguments are delimited by white space, which is either a space or a tab. A string surrounded by double quotation marks is interpreted as a single argument, regardless of white space contained within. A quoted string can be embedded in an argument. A double quotation mark preceded by a backslash is interpreted as a literal double quotation mark. Backslashes are interpreted literally, unless they immediately precede a double quotation mark. If backslashes immediately precede a double quotation mark, every pair of backslashes is interpreted as a literal backslash. If the number of backslashes is odd, the last backslash escapes the next double quotation mark as described in rule 3.
https://docs.python.org/2/library/subprocess.html#converting-argument-sequence