Skip to content

Latest commit

 

History

History
75 lines (56 loc) · 2.04 KB

python.md

File metadata and controls

75 lines (56 loc) · 2.04 KB

Python — Facebook Ad Library Scraper

Call the Facebook Ad Library Scraper from Python with the official apify-client.

Install

pip install apify-client

Run the Actor and read the dataset

from apify_client import ApifyClient

client = ApifyClient("YOUR_TOKEN")

run = client.actor("logiover/meta-ad-library-scraper").call(run_input={
    "searchTerms": ["nike", "running shoes"],
    "country": "US",
    "activeStatus": "active",
    "maxResults": 500,
})

for ad in client.dataset(run["defaultDatasetId"]).iterate_items():
    print(ad["pageName"], "—", ad["status"], "—", ad["adLibraryUrl"])

Pull one advertiser's full ad history by Page ID

run = client.actor("logiover/meta-ad-library-scraper").call(run_input={
    "pageIds": ["15087023444"],
    "country": "US",
    "activeStatus": "all",
    "maxResults": 1000,
})

items = list(client.dataset(run["defaultDatasetId"]).iterate_items())
print(f"Collected {len(items)} ads")

Build a creative swipe file with pandas

import pandas as pd

rows = list(client.dataset(run["defaultDatasetId"]).iterate_items())
df = pd.DataFrame(rows)

swipe = df[["pageName", "bodyText", "ctaText", "linkUrl", "publisherPlatform", "startDate"]]
swipe.to_csv("facebook_ads_swipe.csv", index=False)
print(swipe.head(20))

Political-ad spend tracking

run = client.actor("logiover/meta-ad-library-scraper").call(run_input={
    "searchTerms": ["election"],
    "country": "US",
    "adType": "political_and_issue_ads",
    "maxResults": 500,
})

for ad in client.dataset(run["defaultDatasetId"]).iterate_items():
    if ad.get("spendLowerBound") is not None:
        print(
            ad["pageName"],
            f'{ad["spendLowerBound"]}-{ad["spendUpperBound"]} {ad.get("currency")}',
            ad.get("impressions"),
        )

The client resolves your token, runs the hosted Actor on Apify, and returns structured results. Nothing is scraped locally.