当前位置: 首页 > news >正文

browsermobproxy + selenium 获取接口json

browsermobproxy + selenium 获取接口json数据

第三方库各版本情况:

selenium==4.12.0  browsermob-proxy==0.8.0

browsermob-proxy-2.1.4-bin.zip(外部程序)

1.导入第三方库

# coding=utf-8

import time

from selenium.webdriver.common.by import By

from selenium import webdriver

from selenium.webdriver.firefox.options import Options

import json

from browsermobproxy import Server

2.运行proxy服务器

server = Server('browsermob-proxy-2.1.4\\bin\\browsermob-proxy.bat')

server.start()

proxy = server.create_proxy(params={'trustAllServers':'true'}) #信任所有证书,针对自签名证书

# 创建一个Selenium WebDriver的代理配置  

print(proxy.port)

3.设置options参数

options = Options()

options.add_argument('--headless')

#火狐设置这五行代理就OK了;

options.set_preference("network.proxy.type", 1)

options.set_preference("network.proxy.http", '127.0.0.1') #设置http代理服务器

options.set_preference("network.proxy.http_port", proxy.port) #设置http代理服务器端口

options.set_preference("network.proxy.ssl", '127.0.0.1') #设置https代理服务器

options.set_preference("network.proxy.ssl_port", proxy.port) #设置https代理服务器端口

'''

options.add_argument("--ignore-certificate-errors")

options.add_argument("disable-gpu")

options.add_argument("--disable-extensions")

options.add_argument('--proxy-server={0}'.format(proxy.proxy)) #这四个选项是谷歌的

# 创建一个Firefox配置文件

profile = webdriver.FirefoxProfile()

# 添加受信任的SSL证书 (这部分留个记录)

profile.accept_untrusted_certs = True

profile.assume_untrusted_cert_issuer = False

profile.set_preference("security.enterprise_roots.enabled", True)

profile.set_preference("security.certerrors.recordEventTelemetry", False)

profile.set_preference("security.certerrors.mitm.priming.enabled", False)

profile.set_preference("security.family_safety.mode", 0)

profile.set_preference("security.OCSP.enabled", 0)

profile.set_preference("security.OCSP.require", False)

profile.set_preference("security.ssl.enable_ocsp_stapling", False)

profile.set_preference("security.enterprise_roots.enabled", True)

profile.set_preference("security.enterprise_roots.enabled", True)

profile.add_extension(extension=certificate_file_path)

options.set_capability("moz:firefoxOptions", {"profile": profile})

options.add_argument('--trust-all-certs')

options.add_argument(f'--profile={certificate_file_path}')

'''

4.设置过滤器、创建火狐浏览器实例

#设置代理过滤器

proxy.new_har("test",options={'captureHeaders': True, 'captureContent': True}) 

#services=webdriver.FirefoxService('geckodriver.exe')

driver = webdriver.Firefox(options=options)

5.访问链接获取接口数据(json格式)

#设置url

url='https://xxxx.xx'

driver.get(url)

time.sleep(3)

proxy.wait_for_traffic_to_stop(1, 60)

result = proxy.har

for entry in result['log']['entries']:

    _url = entry['request']['url']

# 根据URL找到数据接口

respose_rul='https://xxxx.xx' #api接口链接特征

    if respose_rul in _url:

        print(_url)

        _response = entry['response']

        _content = _response['content']['text']

        # 返回的均是字符串包着字典,为了提高可读性,先将数据转换成python字典,再转成json数据

        content_dict = json.loads(_content)

        print(content_dict)

        #content_json = json.dumps(content_dict,indent=4)

        #content_json=json.loads(content_json)

        with open('respose.txt','w',encoding='gbk')as f:

            json.dump(content_dict,f,indent=4)

        #print(content_dict)

        # 获取接口返回内容

        #print(_content)

http://www.dtcms.com/a/361176.html

相关文章:

  • PLC操作
  • AI + 机器人:当大语言模型赋予机械 “思考能力”,未来工厂将迎来怎样变革?
  • 森赛睿视觉AI:大模型加持,分类更智能
  • 宋红康 JVM 笔记 Day09|方法区
  • 虚拟化技术是什么?电脑Bios中的虚拟化技术怎么开启
  • 【2025ICCV】Vision Transformers 最新研究成果
  • NetCoreKevin-DDD-微服务-WebApi-AI智能体、AISK集成、MCP协议服务、SignalR、Quartz 框架-14-数据模型与持久化
  • YOLO 目标检测:YOLOv4数据增强、CIoU Loss、网络结构、CSP、SPPNet、FPN和PAN
  • 架构选型:为何用对象存储替代HDFS构建现代数据湖
  • Linux之Shell编程(四)函数、数组、正则
  • 小土堆目标检测笔记
  • 【开题答辩全过程】以 基于Spring Boot的房屋租赁系统的设计与实现为例,包含答辩的问题和答案
  • go语言面试之Goroutine 数量控制, GC回收 和任务调度
  • 【Qwen】Qwen3-30B-A3B 模型性能评估指南 + API KEY介绍
  • DAY02:【DL 第一弹】pytorch
  • JS闭包讲解
  • 在 Halo 中导入 Markdown 和 Word 文档
  • openEuler2403编译安装Nginx
  • 【C++】 Vector容器操作全解析
  • springboot:数据校验
  • 人工智能之数学基础:常用的连续型随机变量的分布
  • Web知识的总结
  • 直播预告 | Excelize 跨语言实战
  • 搭载AX650N高能效比智能视觉芯片——AX2050系列边缘计算盒,可应用在智慧安防交通仓储教育,人脸识别,明厨亮灶,安全生产,智能机器人等
  • Linux ARP老化机制/探测机制/ip neigh使用
  • 前端性能优化实战:如何高效管理和加载图片、字体、脚本资源
  • 数组(4)
  • 重构导航之核:高德地图的深度学习架构解析 导论:从数字化世界到可计算世界
  • TensorFlow深度学习实战(36)——自动机器学习(AutoML)
  • 从能耗黑洞到精准智控:ASCB2智慧空开重构高校宿舍用电能效模型