One of the most critical issues in studying complex networks is detecting communities widely used in sociology, social media (such as Instagram, Twitter, or email networks), biology, physics, data networks, and information technology. Graphs usually implement complex network modelling. In a graph, nodes represent objects, and edges represent connections between objects. Communities are groups of nodes that have many internal connections and few intergroup connections. Although in social network research, the detection of communities has attracted much attention, most of them face functional weaknesses because the structure of the community is not clear or the characteristics of nodes in a community are not the same. Besides, many existing algorithms have complex and costly calculations. In this paper, a model based on Harris Hawk Optimization (HHO) algorithm and Opposition-based Learning (OBL) is proposed for Community Detection (CD). The proposed model uses an OBL to balance exploration and exploitation. The balance between exploration and exploitation is effective in achieving optimal solutions. The evaluation of the proposed model is performed on four different datasets based on modularity criteria and NMI (Normalized Mutual Information). The results show that the proposed model has higher modularity, NMI, and generalizability compared to other methods.