Introduction to Facebook Ad Library
The Facebook Ad Library was opened a few years ago.
Tutorial focuses on scraping ads from the library.
Scraping Ads Overview
Not all ads are scraped, but the method can save and retrieve specific data.
Ads can be accessed by inspecting their source and finding the data.
Inspecting the Ad Source
Start by clicking on the desired ad and inspecting it.
Look for fetch requests that provide the ad data.
Identifying Server-Rendered Data
The data is server-rendered (e.g., using frameworks like Next.js or React).
The data is stored in script tags, often as JSON, making it easier to extract.
Locating the JSON Data
Search within script tags to find JSON data.
Example: Find the ad creative ID or other details within the JSON.
Avoiding Puppeteer or Selenium
Avoid using browser automation tools like Puppeteer or Selenium if JSON is readily available in the script tags.
Directly parse the JSON for simplicity.
Extracting JSON Data
Copy the request using "Copy as Node Fetch" to replicate the data-fetching process.
Parse the HTML response to locate the relevant script tag containing JSON data.
Parsing HTML with Cheerio
Use the Cheerio library to parse HTML and locate specific script tags.
Identify the correct script tag based on the presence of specific keywords like "cards."
Processing the JSON
Extract and slice the JSON from the script tag by determining start and end indices.
Clean up and format the data for use.
Final JSON Data
The extracted JSON contains detailed information, including:
Ad ID
End date
Status (active/inactive)
Ad creative ID
Page name and ID
URL, Instagram handle, spend, and start date.
Benefits of JSON Extraction
Extracted JSON provides much more data compared to raw HTML.
Includes critical metadata like CTAs, captions, and Instagram details.
Key Steps Summarized
Get the HTML of the page.
Parse the script tag to extract JSON data.
Process the JSON to retrieve ad information such as IDs, dates, and other metadata.
Output Example
The JSON includes all data required to analyze the ad, like start/end dates, active status, captions, and spend details.
Conclusion
Scraping the Facebook Ad Library involves fetching HTML, locating JSON in script tags, and parsing it for ad data.
This method is efficient and avoids unnecessary overhead like browser automation tools.
Tools and Techniques Mentioned:
Inspect Element
Fetch Requests
JSON Parsing
Cheerio Library for HTML Parsing
Node Fetch for Replicating Requests
This step-by-step process demonstrates how to scrape and extract valuable ad data from the Facebook Ad Library effectively.
💥 WordPress https://computerclassinsrilanka.wordpress.com
💥 Facebook https://web.facebook.com/itclasssrilanka
💥 Medium https://medium.com/@itclasssl
Comentários