Skip to content

Instantly share code, notes, and snippets.

@dino-su
Last active March 16, 2022 07:37
Show Gist options
  • Save dino-su/29d2646d41acf6aab1546b982a727f42 to your computer and use it in GitHub Desktop.
Save dino-su/29d2646d41acf6aab1546b982a727f42 to your computer and use it in GitHub Desktop.
dump HAR with Selenium and Browsermob-Proxy

Package requirement:

  • browsermob-proxy==0.8.0
  • selenium==3.9.0

Usgae: python dump_har.py 'https://www.google.com' | grep mp3

from browsermobproxy import Server
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from pprint import pprint
def dump_har(url):
# enable headless mode
options = Options()
options.add_argument("--headless")
# enable proxy
server = Server("browsermob-proxy-2.1.4/bin/browsermob-proxy")
server.start()
proxy = server.create_proxy({'captureHeaders': True, 'captureContent': True, 'captureBinaryContent': True})
profile = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
# init browser
driver = webdriver.Firefox(firefox_options=options, firefox_profile=profile)
# dump har
proxy.new_har(url)
driver.get(url)
pprint(proxy.har)
# clean up
server.stop()
driver.close()
if __name__ == "__main__":
import sys
url = sys.argv[1]
dump_har(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment