Shale gas reservoirs have gradually become the main source for oil and gas production. The automatic optimization technology of complex fracture network in fractured horizontal wells is the key technology to realize the efficient development of shale gas reservoirs. In this paper, based on the flow model of shale gas reservoirs, the porosity/permeability of the matrix system and natural fracture system is characterized. The fracture network morphology is finely characterized by the fracture network expansion calculation method, and the flow model was proposed and solved. On this basis, the influence of matrix permeability, matrix porosity, fracture permeability, fracture porosity, and fracture length on the production of shale gas reservoirs is studied. The optimal design of fracture length and fracture location was carried, and the automatic optimization method of complex fracture network parameters based on simultaneous perturbation stochastic approximation (SPSA) was proposed. The method was applied in a shale gas reservoir, and the results showed that the proposed automatic optimization method of the complex fracture network in shale gas reservoirs can automatically optimize the parameters such as fracture location and fracture length and obtain the optimal fracture network distribution matching with geological conditions.