I have a slime farm with an iron golem inside that attracts the slime, but I noticed that it also works with some other mobs if it isn't lit up properly. So I turned afk for a while with light level -7 in the slime farm and a lot of mobs spawned! But when I went down I saw a spider that killed the iron golem. So is there a way to spawn all mobs other than spiders?
I think you have the wrong strategy here.
Many (possibly all?) hostile mobs will attack Iron Golems. The spider may have been the mob to kill it, but all the mobs before the spider likely did damage to the golem, weakening it just enough to finally be killed by the spider.
So, the real problem is not preventing spider spawns, but preventing the golem from dying. Unfortunately, it is not possible to permanently keep it alive. That being said, there are some tricks to keep the golem alive for as long as possible.
Frequently throw splash potions of instant health at the golem. Splash potions of instant health heal golems, but hurt hostile mobs.
Make an iron golem farm, and a water pathway that leads the iron golems to the mobs. This is much more difficult, but a more permanent solution.
If you put fences around the Iron Golem it will stop mobs from getting to it while still attracting the mobs to it.