使用 chrome QUIC 协议 (HTTPS) 导出 HAR 文件

Export HAR file using chrome QUIC protocol (HTTPS)

我正在尝试使用 Chrome 和 QUIC 通过 python 导出 HAR 文件。 使用 TLS1.3 的 QUIC 协议 - 所以我只能使用带有客户端证书的协议

基本上,我想将这两个代码合并为一个。 使用 chrome 配置文件导出 HAR 文件(以启用 TLS1.3、HTTPS 和 QUIC)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\Users\AtechM_03\AppData\Local\Google\Chrome\User Data\Default")
driver = webdriver.Chrome(executable_path=r'C:\path\to\chromedriver.exe', chrome_options=options)
driver.get("https://www.google.co.in")

from browsermobproxy import Server
from selenium import webdriver
import os
import json
import urlparse

server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()

chromedriver = "path/to/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
url = urlparse.urlparse (proxy.proxy).path
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--proxy-server={0}".format(url))
driver = webdriver.Chrome(chromedriver,chrome_options =chrome_options)
proxy.new_har("http://whosebug.com", options={'captureHeaders': True})
driver.get("http://whosebug.com")    
result = json.dumps(proxy.har, ensure_ascii=False)
print result
proxy.stop()    
driver.quit()

我认为实现这种情况(使用 Chrome 默认配置文件捕获 HAR 文件)的最简单方法是在 Linux。从以下设置 link:

https://github.com/cyrus-and/chrome-har-capturer

安装后,您可以从命令行启动 chrome,如下所示:

google-chrome --remote-debugging-port=9222

使用命令抓取HAR文件(将HAR文件保存到当前路径):

chrome-har-capturer -o <filename> <website>

您可以使用 'os' 和 'subprocess' 库在 python 中编写一个短程序:

import os
import subprocess
import time

CHROME_TYPE = "google-chrome --remote-debugging-port=9222"
FILE_NAME = "example.har"
WEBSITE = " https://www.youtube.com"

def main():
    # open new terminal for 'open chrome' command
    browserProcess = subprocess.Popen(CHROME_TYPE, stdout=subprocess.PIPE, shell=True)
    # wait for the chrome window opening
    time.sleep(3)
    # the action include only one line
    os.system("chrome-har-capturer -o "+FILE_NAME+WEBSITE)
    # optional:
    browserProcess.kill()

if __name__ == "__main__":
    main()