Browsermob-Proxy 备忘
1、下载Browsermob-Proxy到本地
下载地址:
百度云盘地址:https://pan.baidu.com/s/1jx2xVkFDMDL2nr4ulWCNuQ 提取码: re8x
Github:https://github.com/lightbody/browsermob-proxy
下载后解压,代码中需要用到 browsermob-proxy.bat 文件
2、安装Browsermob-Proxy的python包
pip install browsermob-proxy
3、实例代码
import time from browsermobproxy import Server from selenium import webdriver from selenium.webdriver.chrome.options import Options if __name__ == '__main__': # 开启代理 BMPserver = Server(r'C:\browsermob-proxy-2.1.4\bin\browsermob-proxy.bat') BMPserver.start() BMPproxy = BMPserver.create_proxy() # 配置代理启动webdriver chrome_options = Options() # 禁用扩展插件 chrome_options.add_argument('--ignore-certificate-errors') # https需要加上,要不然回报安全连接问题 chrome_options.add_argument('--ignore-urlfetcher-cert-requests') chrome_options.add_argument('--proxy-server={}'.format(BMPproxy.proxy)) # 不显示浏览器窗口 chrome_options.add_argument("--headless") dr = webdriver.Chrome(options=chrome_options) BMPproxy.new_har("kgdxpr", options={'captureContent': True, 'captureContent': True, 'captureBinaryContent': True}) url = "https://www.youzy.cn/tzy/search/colleges/collegeList" dr.get(url) # 手动加睡眠,等待整个页面的请求加载完成 time.sleep(2) result = BMPproxy.har for entry in result['log']['entries']: # 打印页面所有调用的地址 # print(entry['request']['method'], entry['request']['url']) entry_url = entry['request']['url'] # 根据URL找到数据接口 if "/youzy.dms.basiclib.api.college.ranking.byyear.query" in entry_url: if str(entry['request']['method']) == 'POST': _response = entry['response'] print('****************************') print(_response['content']['text']) print('****************************') else: continue BMPproxy.close() BMPserver.stop()