I think that it had much to do with the fact that many Spaniards, many more than those who accepted Napoelon and his puppet Joseph, whether liberal or conservative in outlook, were insistent on defending their country against invasion.
The Spanish were defeated, yes - and often. But they came back - time and again. And it was this heroic and stubborn opposition (both in a formal military sense and in terms of a more “popular” resistance) that enabled the eventual defeat of Napoleon. For example, the continued struggles in Spain helped ensure the successful defence of Portugal: for one thing not enough troops could be committed to ensure the attempted invasion was successful.
This spirit of defiance is nowhere better exemplified than in the following ...
Capitulate? ... I do not know how to capitulate! I do not know how to surrender! After I am dead, we will speak of it!” ... “what was Madrid? It was a city like any other and that is no reason for Saragossa to surrender” . Palofox's response to Moncey quoted in Rudorff, R, (1974) War to the death: the sieges of Saragossa 1808 - 1809 pp 192-3
Like a certain series of defensive measures undertaken to prevent the invasion of French territory, by foreign powers who reacted to some very specific events that took place in France from 1789, this was a "people's war".
Perhaps this is one factor of the war in Spain (and Portugal for that matter) that Napoleon failed to properly grasp?
Best wishes
Anthony