首页 > 解决方案 > 使用 chrome QUIC 协议 (HTTPS) 导出 HAR 文件

问题描述

我正在尝试通过 python 使用 Chrome 和 QUIC 导出 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://stackoverflow.com", options={'captureHeaders': True})
driver.get("http://stackoverflow.com")    
result = json.dumps(proxy.har, ensure_ascii=False)
print result
proxy.stop()    
driver.quit()

标签: python-3.xproxyselenium-chromedriverhar

解决方案


我认为实现这种场景(使用 Chrome 默认配置文件捕获 HAR 文件)的最简单方法是在 Linux 中使用chrome-har-capturer命令。从以下链接进行设置:

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

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

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

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

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

您可以使用“os”和“子进程”库在 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()

推荐阅读