Hi! I’m a graduate student studying Mexican and Spanish history in the early 20th century. I’m pulling my conclusions from several good books by Stanley Payne.
https://yalebooks.yale.edu/book/9780300110654/collapse-spanish-republic-1933-1936
The short answer is that both republics were sort of accidents, and neither of them won consensus from enough of society.
The first one arose because the monarch resigned, and liberals in Madrid declared it. It had almost no popular support, and ongoing chaos in the country prevented it from consolidating. When the opportunity arose to invite a new and younger Bourbon (the royal family in Spain) to the throne, the army and many in government welcomed it.
The second republic followed a military government. In 1923 General Primo de Rivera overthrew the civilian government with support from the King and army. In 1930 his government collapsed as the army lost faith in his ability to keep order and the people were suffering from the Great Depression (though to a lesser degree than in more industrialized countries.) The king ordered new elections, and when Republicans (liberals and socialists) won elections in the cities that voted first, the king fled the country. If he would have stayed to see the results from the countryside he would have found out that monarchists won the popular vote. Regardless, the Republican victors declared a Republican government and wrote a Jacobin (far left Liberal) constitution.
Over the course of the next 5 years, the government changed hands several times between a Socialist-Liberal alliance and the new mass Catholic party CEDA. This is a simplification of complex government schemes, but it’s a sufficient outline. The people of Spain increasingly moved to the Left or Right as their governments failed to meet their expectations.
The people on the Left wanted to radically cut the Church out of society. They also wanted big economic reforms like seizing most large farms. No liberal-socialist government was able to accomplish this, and so people began in small numbers to join the Communists and to take direct action. Those on the right wanted the Church to have a central role in society which the Constitution fundamentally prohibited, and they wanted the government to suppress what they considered to be the disorderly mob.
When generals began to conspire in the interest of preventing Communist revolution, they found many allies in Spain.
So in short, the Second Republic failed because the people of Spain wanted irreconcilable goals and the regime satisfied no one.
As a disclaimer, there will be people who disagree with Payne and I. The Second Republic became an Anti-Fascist symbol in the 30s and 40s, but as you can tell from my narrative, there were not many fascists or communists active in the country. Professional historians will typically give the same narrative as Payne, but many Leftist academics and activists will give a different narrative. It’s more confusing because during the Civil War the Left called all of their opponents “Fascists” and the Right called all of their opponents “Reds” or “Communists” even when this wasn’t true. I’m not saying this to smear them. Leftists who disagree with me. The ensuing Franco regime at the very least flirted with fascism and committed atrocities. It is certainly valid to have strong feelings about who was “right” in the Second Republic.