【问题标题】:Export HAR file using chrome QUIC protocol (HTTPS)使用 chrome QUIC 协议 (HTTPS) 导出 HAR 文件
【发布时间】:2019-11-26 15:23:48
【问题描述】:

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

  • 我找到了this 的答案(使用代理服务器),但代理没有启用 QUIC 的客户端证书,因此所有 HAR 文件都是 HTTP1.1 协议。
  • 我找到了this 代码(使用 chrome 配置文件设置),但我无法使用此代码导出 HAR 文件。

基本上,我想将这两个代码合并为一个。 使用 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.x proxy selenium-chromedriver har


    【解决方案1】:

    我认为实现此场景(使用 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'和'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()
    

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 2020-02-23
      • 1970-01-01
      • 2021-05-28
      • 2021-03-30
      • 1970-01-01
      相关资源
      最近更新 更多