Scrape and export the ads published by a Facebook page

I recently wanted to do some research on political advertising and Facebook. I wanted to use the Facebook Ads Library (the tool from Facebook that allows you to see all the ads being published by any advertiser in your country) to get insights about political advertising on Facebook. After exploring the tool, I concluded that it was really hard to download all the ads, especially if you want to go beyond your country or if you want a lot of data. This is why I wrote this Python script without any clever web scraping: it simply goes through every page of search results from Facebook and exports all the ad information as a JSON file.

Facebook has recently launched a great tool to find any political ads anywhere in the world, in every country.

Facebook has recently launched a great tool to find any political ads anywhere in the world, in every country. The tool is called the Facebook Ads Library and it can be accessed by anyone at facebook.com/ads/library/.

You can search for an advertiser, run a search by keyword and explore what kind of ads are being shown in your area.

The tool is called the Facebook Ads Library and allows you to search for an advertiser, run a search by keyword and explore what kind of ads are being shown in your area.

You can use the tool to search for an advertiser, run a search by keyword and explore what kind of ads are being shown in your area. The Facebook Ads Library allows users to filter their results based on their location and industry.

The feature will show you all the ads that have been published by the page that matches your input parameters. This means if you want to see all of the ads from companies selling shoes in Boston, then type “shoe” into the ad library search bar along with your city name. If there are no results, it means this company does not have an active page anymore or is not advertising at all in Boston right now.

I wanted to use this tool to get insights about political advertising on Facebook.

The Facebook Ads Library is a tool that allows you to see all of the ads that have been published by a particular page. This type of information can be useful for many purposes: for example, if you want to understand what types of ads different companies are running on Facebook and how they're doing so. Or perhaps you want to get some insight into political advertising on the platform.

If you're interested in learning more about the tool, let's dive right in!

After exploring the tool, I concluded that it was really hard to download all the ads, especially if you want to go beyond your country.

I started my research by trying to scrape the ads published by a Facebook page. After exploring the tool, I concluded that it was really hard to download all the ads, especially if you want to go beyond your country.

The parameters you can use with this feature are pretty limited and not very flexible:

  • Number of days: 1 - 90

  • Start date: This is the first day of your campaign (you must fill in) or today's date if you want to download all your campaigns from today onwards (this option can be useful if you're working on an ongoing project)

  • End date: The last day of a campaign or yesterday's date if it's an ongoing one (see above).

This is why I wrote this Python script without any clever web scraping: it simply goes through every page of search results from Facebook and exports all the ad information as a JSON file.

If you're a fan of Python, you might also be interested in this script. It was written to scrape all the ads from Facebook's Ad Library and save them as JSON files, so I could use them for analysis.

I just wanted something that would download all the ads from the Facebook Ad Library and save them as a JSON file. The script does that. You can find it here: https://github.com/pdxmba/scrapetheads

You can see at the bottom of this post some visualizations I did with Tableau and the data set I got from running my script for several pages of results and different countries.

You can also see at the bottom of this post some visualizations that I did with Tableau and the data set I got from running my script for several pages of results and different countries.

How does my script work? It simply uses a Java library called Selenium to open a Firefox window, goes through all the pages of the Facebook Ad Library, clicks on every ad and adds its information (advertiser name, text, image…) into a JSON file.

You can imagine that my script works like this:

  • It simply uses a Java library called Selenium to open a Firefox window, goes through all the pages of the Facebook Ad Library, clicks on every ad and adds its information (advertiser name, text, image…) into a JSON file.

My mindset while creating this script was pretty simple: make it as simple as possible, rather than trying something fancy (e.g AI) and failing miserably at the end…

The good news is that you don't need any fancy AI technology to scrape Facebook pages. In fact, the solution I built was extremely simple and easy to implement, test and modify. It's also quite readable as well.

The whole process consisted of three steps:

  • Scrape Facebook pages for ads

  • Parse HTML code from these pages

  • Export data from HTML files into a CSV file

Simple solutions are often better

There are many reasons why simple solutions are often better. Here's a list of reasons why:

  • Simple solutions are more reliable, because they're easier to maintain and understand.

  • Simple solutions have fewer bugs, which means developers spend less time debugging them.

  • Simple solutions can be implemented faster (which means the app will launch sooner).

Conclusion

I hope this was helpful to some of you. I’m definitely not an expert in web scraping but I hope my script will help those who aren’t either and want to use the Facebook Ad Library for research purposes or other reasons. As said earlier, if you have any suggestion about how to improve it, don’t hesitate to let me know!

Previous
Previous

How To Better Manage Your Sales and Marketing Collateral

Next
Next

Tips for Scheduling More Business Meetings