Idealista and all the real estate companies are the morons feeding the problem by putting the country for rent. They’re the reason people can’t afford a house and we have to talk about okupas in the first place.
First, I think okupas are a very small problem, mainly used for propaganda. But how are real state companies feeding the problem? The problem is that there's no houses available, because politicians create artificial scarcity. In any local government, the stronger department is "Urbanism", decides if you get rich by allowing you to develop on your property, or you stay poor because you don't get the permits. Come on,…
There's an easy way to fix that: create a tax for unoccupied housing. If real estate sits unused for extensive durations instead of having tenants in them (i.e. having a rent low enough someone can afford it) it will burn a hole in your pocket. If you want to sell instead of letting someone rent, you will be incentivized to sell it ASAP even if you have to lower your price or make a loss.
Preferring to sell rather than renting out doesn't create a shortage. Preferring to keep housing unoccupied (in order to sell it) rather than renting it out or selling it at a price someone can afford does. If the market can't connect buyers/renters and sellers/landlords because the former can't afford the prices set by the latter and there's no economic pressure on the latter to lower their prices, you can just create that pressure.
Of course this would disincentivize private housing construction for people who don't also plan to live in that housing themselves but there's no reason there can't be a publicly funded organization for housing development able to take losses on sales/rent because it is backed by public money. This isn't uncharted territory either.
There's no reason housing has to function as an unregulated commodity. There's especially no reason to believe we can approximate that without further feeding into the housing crisis. There's a reason we have the term "rent-seeking" and why it has negative connotations: landlords only exist because most people can't afford or aren't eligible for the kind of loan that would allow them to build or buy a house. Unlike loan payments which end once you've paid off the loan (plus interest), rent goes on forever and only ever goes up. The entire point of being a landlord is that the rent accumulates to a sum greater than what you paid for the property (plus interest if you had to get a loan). Landlords literally don't add value. They're more like scalpers.