I think the spiders have not so much figured it out as there is a selection bias. Spiders randomly make their nests. The spiders that randomly make a nest near a light source end up with a larger supply of food and grow large. In my current garden, the spiders that end up near a particular fern always grow the largest. My brother used to feed spiders that made their webs in a particular corner of a cabin as a reward for the entertainment they provided when they were building their webs. I have also walked into countless webs from large spiders in the forest over my life.
Everything points to just randomized locations but being near lights generally being good for the lucky spiders that end up there. You will often see spider fights in such locations after a larger moth or other insect breaks the web of one spider and drops it into another spiders web.
You found streetlights in the Peruvian rainforest?
It seems like the ants stand on the lamps and wait to catch other insects that fly by then strike in unison. It was an incredible strategy.
I once let a small moth-like flying insect live free in my house and later discovered the damned thing had laid eggs in every package of dried plant food in my cupboard, including a few glass jars I thought were airtight, and were crawling with maggots.
As I tossed most of my stored food, I swore to never let another insect in the house again. Praise spiders.
I have small frogs that climb on the windows in the summer to feast on the bugs attracted to the inside lights. It is wonderous and hilarious to see