浏览器驱动配置

2020-04-20  本文已影响0人  Canon_2020
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-04-15 09:00:00
# @Author  : Canon
# @Link    : https://www.python.org
# @Version : 3.6.1

from browsermobproxy import Server


def get_driver(proxy):
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--ignore-certificate-errors')
    chrome_options.add_argument('--proxy-server={host}:{port}'.format(host='localhost', port=proxy.port))
    options = {"profile.managed_default_content_settings.images": 2}
    chrome_options.add_experimental_option("prefs", options)
    chrome_options.add_argument('--disable-gpu')
    return webdriver.Chrome(chrome_options=chrome_options)


# 下载 browsermobproxy 部署版本: http://bmp.lightbody.net/
proxy_path = "C:/Users/canonchen/Downloads/browsermob-proxy-2.1.4/bin/browsermob-proxy.bat"
cls.server = Server(proxy_path)
cls.server.start()
cls.proxy = cls.server.create_proxy()
# 开启代理监控,如果不监控会拿不到请求内容
cls.proxy.new_har("monitor", options={
    'captureContent': True,
    'captureHeaders': True
})
# 浏览器驱动
cls.driver = get_driver(cls.proxy)

result = self.proxy.har['log']['entries']
print(result)


cls.driver.close()
cls.driver.quit()
try:
    cls.proxy.close()
    cls.server.process.terminate()
    cls.server.process.wait()
    cls.server.process.kill()
except OSError:
    pass

上一篇 下一篇

猜你喜欢

热点阅读