如何使用 python 在 PhantomJS 中安装 SSL 证书?
what is the correct way to feed an ssl certificate into phantomjs
在命令行中说,使用--ssl-certificates-path
。
最佳答案
编译答案«what is the correct way to feed an ssl certificate into phantomjs»和 «Is there a way to use PhantomJS in Python?»并考虑到您没有提到 Selenium,我想您的 python 脚本看起来与此类似:
command = "phantomjs --ignore-ssl-errors=true --ssl-client-certificate-file=C:\tmp\clientcert.cer --ssl-client-key-file=C:\tmp\clientcert.key --ssl-client-key-passphrase=1111 /path/to/phantomjs/script.js"
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
# make sure phantomjs has time to download/process the page
# but if we get nothing after 30 sec, just move on
try:
output, errors = process.communicate(timeout=30)
except Exception as e:
print("\t\tException: %s" % e)
process.kill()
# output will be weird, decode to utf-8 to save heartache
phantom_output = ''
for out_line in output.splitlines():
phantom_output += out_line.decode('utf-8')
从 Python 使用 PhantomJS 的另一种方法是借助 Selenium 自动化工具。这样您还必须通过 CLI 提供必要的证书文件:
from selenium import webdriver
driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '--ssl-client-certificate-file=C:\tmp\clientcert.cer', '--ssl-client-key-file=C:\tmp\clientcert.key', '--ssl-client-key-passphrase=1111'])
driver.set_window_size(1280, 1024)
driver.get('https://localhost/test/')
driver.save_screenshot('screen.png')
driver.quit()
请注意,从 2.1 版开始,提供自定义 ssl key 在 PhantomJS 中有效。
关于python - 如何在 Python phantomjs 中安装 SSL 证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40600745/