본문으로 바로가기

IE && Chrome browsermobproxy setting

category Backend/Python 2020. 5. 21. 11:21
반응형
# -*- coding: utf-8 -*-

from browsermobproxy import Server
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import pprint, time, base64, json

save_path = "if you need save path, insert path"

class SeleniumObject:

    def __init__(self):
        # Start up the server
        self.__server = Server("browsermobproxy bat path")
        self.__client = None

    def start_server(self):
        self.__server.start()
        return self.__server

    def start_client(self):
        self.__client = self.__server.create_proxy(params={"trustAllServers": "true"})
        return self.__client

    @property
    def client(self):
        return self.__client

    @property
    def server(self):
        return self.__server

    def setupDriver(self):
        PROXY = self.client.proxy

    # IE Settings Start
        caps = DesiredCapabilities.INTERNETEXPLORER.copy()
        caps["proxy"] = { "proxyType": "manual", "httpProxy": PROXY }

        self.driver = webdriver.Ie("ie webdriver path", capabilities=caps)
    # IE Settings End

        # Proxy Capture Start
        self.__client.new_har("something name", options={'captureHeaders': True, 'captureContent': True, 'captureBinaryContent': True})

        self.driver.get('something url')

        time.sleep(10)

        json_data = self.__client.har

        entries = json_data['log']['entries']

        print MyPrettyPrinter().pformat(entries)

        # result_list = []

        # for i in range(0, len(entries)):
        #     url = entries[i]['request']['url']
        #     mimeType = entries[i]['response']['content']['mimeType']
        #     if url == "something url" and mimeType == "something mimeType":
        #         response = entries[i]['response']['content']['text']

        #         if response:
        #             something ~~

        # with open('test.json', 'w') as make_file:
        #     json.dump(result_list, make_file, ensure_ascii=False)

        self.driver.close()

class MyPrettyPrinter(pprint.PrettyPrinter):
    def format(self, _object, context, maxlevels, level):
        if isinstance(_object, unicode):
            return "'%s'" % _object.encode('utf8'), True, False
        elif isinstance(_object, str):
            _object = unicode(_object,'utf8')
            return "'%s'" % _object.encode('utf8'), True, False
        return pprint.PrettyPrinter.format(self, _object, context, maxlevels, level)

selenium = SeleniumObject()
server = selenium.start_server()
client = selenium.start_client()
selenium.setupDriver()
server.stop()

options = webdriver.ChromeOptions()
options.add_argument("--proxy-server={}".format(PROXY))
options.add_argument("--ignore-certificate-errors") # 인증서 에러 로그 숨김
options.add_argument("--ignore-ssl-errors") # ssl 에러 로그 숨김
options.add_argument("--start-maximized") # 창 최대

driver = webdriver.Chrome("webdriver path", chrome_options=options)

Chrome은 IE Settings 주석 내용을 교체하면된다


browsermob proxy bat는 https://bmp.lightbody.net/ 에서 다운로드 가능하다

반응형