• 【selenium自动化过程中的api抓包】browsermobproxy的安装和配置


    第一步

    pip install browsermob-proxy
    
    • 1

    先装一个包

    第二步

    要求jdk1.8以上的环境变量依赖
    ps:如果是刚配的环境记得,重启一下
    在这里插入图片描述

    第三步

    下载java端BrowserMob-Proxy包:http://bmp.lightbody.net/
    在这里插入图片描述
    直接拿来主义(在py配置的时候要记得写上browsermob-proxy.bat地址)

    实验:百度一下

    在这里插入图片描述
    比如说,我想随便抓一个js的response看看

    在这里插入图片描述
    确实可以看到一些粗略的内容,细致的js代码还需要探索一下

    完整代码

    from browsermobproxy import Server
    from selenium import webdriver
    import time
    import pprint
    
    class ProxyManger:
    
        __BMP = r"C:\Users\Irving\Desktop\echain\browsermob-proxy-2.1.4-bin\browsermob-proxy-2.1.4\bin\browsermob-proxy.bat"
    
        def __init__(self):
    
            self.__server = Server(ProxyManger.__BMP)
            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
    
    if __name__=="__main__":
        # 开启Proxy
        proxy = ProxyManger()
        server = proxy.start_server()
        client = proxy.start_client()
    
        # 配置Proxy启动WebDriver
        options = webdriver.ChromeOptions()
        options.add_argument("--proxy-server={}".format(client.proxy))
        options.add_argument('--ignore-certificate-errors')
        options.add_argument('--headless')
        #chromePath = r"D:\AzRjN\anaconda3_7\envs\demo36\Lib\site-packages\selenium\webdriver\chrome\chromedriver.exe"
        driver = webdriver.Chrome(chrome_options=options)
    
        # 获取返回的内容
        client.new_har("baidu.com", options={'captureHeaders': True, 'captureContent': True})
        driver.get("https://www.baidu.com/")
        time.sleep(3)
    
        result = client.har
        print(result)
    
        for entry in result['log']['entries']:
            _url = entry['request']['url']
            print("请求地址:", _url)
    
            if "https://pss.bdstatic.com/r/www/cache/static/protocol/https/amd_modules/san/dist/san_5017f11.js" in _url:
                _response = entry['response']
                _content = _response['content']
                print("---------https://pss.bdstatic.com/r/www/cache/static/protocol/https/amd_modules/san/dist/san_5017f11.js 请求响应内容:", _response)
    
        server.stop()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    总结

    selenium一边自动化操作
    browsermobproxy一边抓包

  • 相关阅读:
    论文阅读-Whisper语音识别(OpenAI)
    QT:QML中使用Loader加载界面
    【2024-04-24】华为春招笔试三道编程题解
    关系代数表达式练习(针对难题)
    C++ 重载运算符和重载函数
    实验五:图像傅里叶变换
    Python武器库开发-flask篇之error404(二十七)
    03.webpack中hash,chunkhash和contenthash 的区别
    【gpt实践】50个提升工作效率的GPT指令
    什么是RTMP 和 RTSP?它们之间有什么区别?
  • 原文地址:https://blog.csdn.net/weixin_40986490/article/details/125500742