They almost always atack the same planet or another that you conquered more close to that. Just have 3 or more hangars (not less) and upgrade them earlier if there is it. Build only bombers at the hangar. Remember then something: when you are being atacked by anything, you can and many times should build things while being atacked. Most times, and pirates I'm sure, won't just go to destroy your builder ship, they don't bother with that, so you should build the plataform defenses close to where the group are (when they stop to atack something) to liquidate them faster. Just build 2 or 3 close by and let those right there after destroying the enemy. But hangars are always important because they are more practical and reliable, tough not has powerful.
Pirates are only a problem if you get all unprepared for them. To have more hangars and defenses, you need to always remember to increase the tactical slots of the planet and look to only and to fortify to the max the planets that are in rout of atack (unless you have plenty of money, but it's good to have at least one hangar in most planets). And you see that, when you get atacked there one first time because it will be again much probably (by the AI anyways and always true for pirates).
A single capital ship can hold a pirate group for long and get some cool leveling, but need a hangar or two to back it up. Pirates are your friends, not only because of that, but they also force you to protect some planets that might be atacked by stronger forces in the future.
Try make a trade port (they have a fetish to go destroy it) and put some plataform defense (ex beams) close to it too. Beams are good close to each other, not scattered arround.