【原创】Selenium获取请求头、响应头-腾讯云开发者社区-腾讯云
selenium 4.0.0
selenium-wire 5.1.0
python 3.10
from seleniumwire import webdriver
import time
from selenium.webdriver.common.by import By
import re
def get_request_headers(driver):"""获取请求头headers详细信息"""print('获取所有请求headers:', len(driver.requests))for request in driver.requests:print('请求headers:')print(request.headers)print('=======================')def get_request_urls(driver):"""获取所有加载的url"""print('获取所有加载的url:',len(driver.requests))for request in driver.requests:print(request.url)def get_response_headers(driver):"""获取响应头headers详细信息"""for request in driver.requests:print('响应headers:')print(request.response.headers)def get_request_urlheaders(driver):print('获取指定url的header:', len(driver.requests))for request in driver.requests:if '/bomp-logdata-adapter/nlpData/getNlpList' in request.url:print(request.url)print(request.headers)timestamp = re.findall(r"timestamp: ([0-9]+)", str(request.headers))[0]print("timestamp:"+timestamp)sign = re.findall(r"sign: ([0-9a-zA-Z]+)", str(request.headers))[0]print("sign:"+sign)if __name__ == '__main__':driver = webdriver.Chrome()driver.get('https://xxxx/aiservice/html/zhuque/index.html#/basic/welcome')time.sleep(25)driver.find_element(By.XPATH, "//li[3]/div/span/span/span[2]").click()time.sleep(5)driver.find_element(By.XPATH, "//li[3]/ul/li[2]/span/span").click()time.sleep(3)# driver.find_element(driver, (By.ID, 'userData')).click()# get_request_urls(driver)get_request_urlheaders(driver)time.sleep(3)driver.quit()
注:xpath 路劲的获取是通过selenium ide 录制获取的