Next time this happens (when it's happening) try to diagnose what's causing the error. Here are some commands you can run in terminal (while it's not working) and what they mean in relation to the failure.
1.
Code:
arp -a | head -n1 | cut -d\( -f2 | cut -d \) -f1 | xargs ping -c5
If the output is "5 packets transmitted, 5 packets received" then the problem isn't the WiFi per-se - your computer can access your network fine, so go to 2. If you see "request timed out" then there is a problem with the WiFi itself. You don't need to bother with any more tests here. If you get any other errors with this command, then your computer doesn't have a default router, which suggests it is losing its IP address.
2.
If you see "5 packets transmitted, 5 packets received" again, then it's the browser at fault (your internet connection is fine). If you see "5 packets transmitted, 0 packets received" then there is likely a firewall or something causing the problem. If you see no route to host, then there is likely an issue with your WiFi hardware or your router. If you get "Unknown host", go to 3.
3.
If you see "5 packets transmitted, 5 packets received", your issue is DNS related so go to 4. If you get any other output, then your computer can't access the internet at all.
4.
This will test your default DNS server. If this command fails, go to 5. If the command works OK, then we need to do some more diagnostics with OS X's resolver.
5.
Code:
nslookup google.co.uk 8.8.8.8
This will test a DNS lookup with a different DNS server. If this works but 4 doesn't, you need to change your DNS server to something more reliable.
These aren't fixes, but should give us more of an idea of what's actually going wrong.