python-3.x - 使用 chrome QUIC 协议 (HTTPS) 导出 HAR 文件
问题描述
我正在尝试通过 python 使用 Chrome 和 QUIC 导出 HAR 文件。使用 TLS1.3 的 QUIC 协议 - 所以我只能使用带有客户端证书的协议
- 我找到了这个答案(使用代理服务器),但代理没有启用 QUIC 的客户端证书,因此所有 HAR 文件都是 HTTP1.1 协议。
- 我找到了这段代码(使用 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()
解决方案
我认为实现这种场景(使用 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()
推荐阅读
- chronicle - 如何在两个不同的线程中使用相同的 CQ
- python - 创建回文程序时出现字符串索引超出范围错误
- python - 使用 numpy 进行智能索引
- python - 如何在 pygame 的屏幕顶部显示分数?
- amazon-web-services - Kafka EC2:“advertised.listeners”和“listeners”的配置问题
- oracle - 添加相同值的日期以显示为单个日期 PL/SQL
- android - 以编程方式为所有用户卸载 APK
- oop - 这种装饰器模式的解决方案是什么?
- python - scikit learn中RandomizedSearchCV和KNeighborsClassifier中n_jobs=-1无效的可能原因和解决方案是什么?
- python - 在 S3 文件上运行 Python 脚本