반응형
# -*- 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/ 에서 다운로드 가능하다
반응형
'Backend > Python' 카테고리의 다른 글
INSERT, SELECT, UPDATE, DELETE 샘플 쿼리 코드 조각 생성기 (0) | 2020.11.22 |
---|---|
WebDriver & Selenium 모든 팝업창 닫기 (1) | 2020.05.01 |
ftp 업로드 후 파일 사이즈 0 (ftp upload file size 0) (2) | 2019.12.07 |
라이브러리를 이용하여 엑셀 데이터 읽기 (0) | 2019.12.05 |
유니코드 인코딩 쉽게 (0) | 2019.11.21 |