• Selenium 获取请求响应


    '''
    Python 3.7
    selenium==3.141.0
    urllib3==1.26.2
    Chromium 109.0.5405.0 (32 位) 
    '''
    1. import json
    2. from selenium import webdriver
    3. from selenium.common.exceptions import WebDriverException
    4. import time
    5. options = webdriver.ChromeOptions()
    6. # 谷歌浏览器位置
    7. chrome_location = r'D:\\Program Files (x86)\\Google\Chrome\\Application\\chrome.exe'
    8. # 谷歌浏览器驱动地址
    9. chromedriver_path = r'D:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver_win32\\chromedriver.exe'
    10. options.binary_location = chrome_location
    11. ###################################################################################
    12. # 写法一
    13. # (网上还有其他的方法,有的会报错 可能是版本问题,selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: log type 'performance' not found,
    14. # 下面两种测试正常)
    15. caps = {
    16. 'browserName': 'chrome',
    17. 'version': '',
    18. 'platform': 'ANY',
    19. 'goog:loggingPrefs': {'performance': 'ALL'},
    20. 'goog:chromeOptions': {'extensions': [], 'args': ['--headless']}
    21. }
    22. caps = {
    23. "browserName": "chrome",
    24. 'goog:loggingPrefs': {'performance': 'ALL'}
    25. }
    26. driver = webdriver.Chrome(executable_path=chromedriver_path, options=options, desired_capabilities=caps)
    27. ###################################################################################
    28. ###################################################################################
    29. # 写法二 (建议用这种,selenium 4 测试也行)
    30. # options.set_capability("goog:loggingPrefs", {"performance": "ALL", "browser": "ALL"})
    31. # driver = webdriver.Chrome(executable_path=chromedriver_path, options=options)
    32. ###################################################################################
    33. # 查询的 IP
    34. list_query = ['135.89.67.33', '34.66.45.22']
    35. for query in list_query:
    36. driver.get(f'http://ip-api.com/json/{query}')
    37. # 等待所有请求完成,可以用等待界面元素方法
    38. time.sleep(10)
    39. logs = driver.get_log("performance")
    40. for item in logs:
    41. # print(item)
    42. log = json.loads(item["message"])["message"]
    43. # if "Network.response" in log["method"] or "Network.request" in log["method"] or "Network.webSocket" in log["method"]:
    44. # pprint(log)
    45. if log["method"] == 'Network.responseReceived':
    46. url = log['params']['response']['url']
    47. if url == 'data:,': # 过滤掉初始data页面,后续可以根据 log['params']['response']['type']过滤请求类型
    48. continue
    49. print('请求', url)
    50. request_id = log['params']['requestId']
    51. response_headers = log['params']['response']['headers']
    52. status_code = log['params']['response']['status']
    53. try:
    54. request_data = driver.execute_cdp_cmd('Network.getRequestPostData', {'requestId': request_id})
    55. except WebDriverException: # 没有后台数据获取时会有异常
    56. request_data = None
    57. response_body = driver.execute_cdp_cmd('Network.getResponseBody', {'requestId': request_id})['body']
    58. print('响应', response_body)
    59. '''
    60. 输出:
    61. 请求 http://ip-api.com/json/135.89.67.33
    62. 响应 {"status":"success","country":"United States","countryCode":"US","region":"IN","regionName":"Indiana","city":"Indianapolis","zip":"46204","lat":39.7709,"lon":-86.1585,"timezone":"America/Indiana/Indianapolis","isp":"AT\u0026T Services","org":"AT\u0026T Services, Inc.","as":"","query":"135.89.67.33"}
    63. 请求 http://ip-api.com/json/34.66.45.22
    64. 响应 {"status":"success","country":"United States","countryCode":"US","region":"IA","regionName":"Iowa","city":"Council Bluffs","zip":"","lat":41.2619,"lon":-95.8608,"timezone":"America/Chicago","isp":"Google LLC","org":"Google Cloud (us-central1)","as":"AS396982 Google LLC","query":"34.66.45.22"}
    65. '''
    '''
    Python 3.8
    selenium==4.21.0
    urllib3==2.2.2
    Chromium 109.0.5405.0 (32 位) 
    '''
    1. from selenium import webdriver
    2. from selenium.webdriver.chrome.service import Service
    3. from selenium.webdriver.chrome.options import Options
    4. from selenium.common.exceptions import WebDriverException
    5. import json
    6. import time
    7. # 谷歌浏览器位置
    8. chrome_location = r'D:\\Program Files (x86)\\Google\Chrome\\Application\\chrome.exe'
    9. # 谷歌浏览器驱动地址
    10. chromedriver_path = r'D:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver_win32\\chromedriver.exe'
    11. # 启用性能日志
    12. options = Options()
    13. options.set_capability("goog:loggingPrefs", {"performance": "ALL", "browser": "ALL"})
    14. options.binary_location = chrome_location
    15. # 启动WebDriver
    16. service = Service(executable_path=chromedriver_path)
    17. driver = webdriver.Chrome(service=service, options=options)
    18. # 查询的 IP
    19. list_query = ['135.89.67.33', '34.66.45.22']
    20. for query in list_query:
    21. driver.get(f'http://ip-api.com/json/{query}')
    22. # 等待所有请求完成,可以用等待界面元素方法
    23. time.sleep(10)
    24. logs = driver.get_log("performance")
    25. for item in logs:
    26. # print(item)
    27. log = json.loads(item["message"])["message"]
    28. # if "Network.response" in log["method"] or "Network.request" in log["method"] or "Network.webSocket" in log["method"]:
    29. # pprint(log)
    30. if log["method"] == 'Network.responseReceived':
    31. url = log['params']['response']['url']
    32. if url == 'data:,': # 过滤掉初始data页面,后续可以根据 log['params']['response']['type']过滤请求类型
    33. continue
    34. print('请求', url)
    35. request_id = log['params']['requestId']
    36. response_headers = log['params']['response']['headers']
    37. status_code = log['params']['response']['status']
    38. try:
    39. request_data = driver.execute_cdp_cmd('Network.getRequestPostData', {'requestId': request_id})
    40. except WebDriverException: # 没有后台数据获取时会有异常
    41. request_data = None
    42. response_body = driver.execute_cdp_cmd('Network.getResponseBody', {'requestId': request_id})['body']
    43. print('响应', response_body)
    '''
    参考:
    https://blog.csdn.net/MXB_1220/article/details/131775148
    https://blog.csdn.net/u014376732/article/details/133973141
    https://www.cnblogs.com/szyicol/p/18093390
    https://www.cnblogs.com/superhin/p/15023302.html
    https://segmentfault.com/q/1010000043296964
    '''
  • 相关阅读:
    企业文化对于营造积极向上的工作氛围有多大影响?
    Volatile应用与底层原理
    【CVPR 2021】Cylinder3D:用于LiDAR点云分割的圆柱体非对称3D卷积网络
    提高APP安全性的必备加固手段——深度解析代码混淆技术
    【不定期更新】什么是Linux内核
    Docker容器学习笔记(看了狂神视频)
    民安智库(第三方市场调查公司)地铁出入口客流量监测,让出行更智能
    麒麟 Kylin V10 一键安装 Oracle 11GR2 单机 ASM(231017)
    项目实战4: 基于 SpringBoot 的超市账单管理系统
    JVM性能调优
  • 原文地址:https://blog.csdn.net/yudiandian2014/article/details/139799804