There are a couple of products for this already: "AdTrap" and for the Raspberry Pi "pihole". (Content warning on https://pi-hole.net/ : contains piping curl into bash, slow-loading SPA site)
1. http://jacobsalmela.com/block-millions-ads-network-wide-with...