Cognitive radio (CR) is an effective technology for addressing spectrum scarcity, which can improve the utilization of spectrum resources through intelligent sensing and dynamic parameter adjustment. Since traditional resource allocation algorithms are difficult to adapt to the dynamic characteristics of cognitive radio environment, more and more researchers are focusing on intelligent optimization algorithms. Our objective is to maximize the channel capacity of cognitive transmitters under interference constraint at primary receiver, total transmit power constraint and fairness constraint in underlay cognitive radio networks. To enhance the flexibility of the algorithm, we transform the original constrained optimization problem into an unconstrained penalty function form. Given that the proposed problem is non-convex, we present the Spider Wasp Optimization (SWO) algorithm to solve this optimization problem. To better search the solution space and avoid getting trapped in local optima, a hybrid Spider Wasp Optimization algorithm (HSWO) is proposed. This algorithm integrates genetic algorithm (GA) principles to help the SWO algorithm in achieving the global optimum. Additionally, three different dynamic response strategies were proposed to validate the adaptability and flexibility of the proposed algorithm in dynamic environments. Simulation results show that HSWO and SWO algorithm can obtain higher system capacity and higher flexibility compared with the particle swarm optimization (PSO).