Consensus algorithms are the core technology of a blockchain and directly affect the implementation and application of blockchain systems. Delegated proof of stake (DPoS) significantly reduces the time required for transaction verification by selecting representative nodes to generate blocks, and it has become a mainstream consensus algorithm. However, existing DPoS algorithms have issues such as “one ballot, one vote”, a low degree of decentralization, and nodes performing malicious actions. To address these problems, an improved DPoS algorithm based on community discovery is designed, called CD-DPoS. First, we introduce the PageRank algorithm to improve the voting mechanism, achieving “one ballot, multiple votes”, and we obtain the reputation value of each node. Second, we propose a node voting enthusiasm measurement method based on the GN algorithm. Finally, we design a comprehensive election mechanism combining node reputation values and voting enthusiasm to select secure and reliable accounting nodes. A node credit incentive mechanism is also designed to effectively motivate normal nodes and drive out malicious nodes. The experimental simulation results show that our proposed algorithm has better decentralization, malicious node eviction capabilities and higher throughput than similar methods.