Skip to main content

socialmediaextractor

Find a website's social media profiles in one call, without picking up share buttons.

PyPI version Python versions PyPI Downloads Tests CodeQL License: MIT Ruff typed: mypy PRs welcome

socialmediaextractor fetches a web page (or takes HTML you already have) and returns the organization's profile links on X/Twitter, LinkedIn, Instagram, Facebook, YouTube, TikTok, GitHub, Threads and Bluesky.

Why

A plain "does the link contain twitter.com?" check also matches share buttons (twitter.com/intent/tweet), single posts, embeds and look-alike domains. This library uses explicit per-platform URL rules instead, reads the identity declarations sites make on purpose (JSON-LD sameAs, rel="me"), and collapses different spellings of the same account, so you get one clean profile URL per network.

Features

9 platforms Facebook, LinkedIn, Twitter (twitter.com and x.com, alias X), Instagram, YouTube, TikTok, GitHub, Threads, Bluesky
Three link sources JSON-LD sameAs, <link href> (for example rel="me"), and <a href>, in that priority order
Profile-only rules Skips share/intent/dialog URLs, posts, reels, videos, GitHub issue pages, site-navigation pages and look-alike hosts
Deduplication x.com/Acme, twitter.com/acme/ and www.x.com/acme?utm_source=... count as one account
URL resolution Relative and protocol-relative (//facebook.com/...) links are resolved against the page URL
Bring your own HTML Every extractor works on an HTML string, so it fits into existing crawlers
Robust fetching Timeout on every request, optional requests.Session, one clear SocialMediaExtractorError
CLI and typing socialmediaextractor command with JSON output; ships py.typed, checked with mypy

Install

pip install socialmediaextractor
# or
uv pip install socialmediaextractor

Requires Python 3.10 to 3.13. Dependencies: requests and beautifulsoup4. No extras.

Quickstart

Offline, on HTML you already have:

from socialmediaextractor import classify_url, extract_all_social_links, extract_social_links_from_html

html = """
<script type="application/ld+json">{"@type": "Organization",
  "sameAs": ["https://www.linkedin.com/company/acme-corp"]}</script>
<a href="https://twitter.com/intent/tweet?text=hi">Share on X</a>
<a href="https://x.com/acmecorp">Follow us</a>
<a href="https://twitter.com/AcmeCorp/">Old link, same account</a>
<a href="//github.com/acme-corp">GitHub</a>
"""

print(extract_social_links_from_html(html, "https://acme.example/", platforms=["X", "LinkedIn", "GitHub"]))
# {'Twitter': 'https://x.com/acmecorp', 'LinkedIn': 'https://www.linkedin.com/company/acme-corp', 'GitHub': 'https://github.com/acme-corp'}

print(extract_all_social_links(html, platforms=["X"]))
# {'Twitter': ['https://x.com/acmecorp']}

print(classify_url("https://twitter.com/intent/tweet?text=hi"))  # None

Fetching a live site (needs network access):

from socialmediaextractor import extract_social_media_accounts

print(extract_social_media_accounts("https://www.djangoproject.com"))

Output (run on 2026-10-31; live sites change):

{'Facebook': None,
 'LinkedIn': 'https://www.linkedin.com/company/django-software-foundation',
 'Twitter': 'https://x.com/djangoproject',
 'Instagram': None,
 'YouTube': None,
 'TikTok': None,
 'GitHub': 'https://github.com/django/django',
 'Threads': None,
 'Bluesky': 'https://bsky.app/profile/djangoproject.com'}

Every requested platform key is always present; platforms with no link are None.

Command line

socialmediaextractor https://www.python.org
socialmediaextractor https://www.mozilla.org --all -p Bluesky -p Instagram
socialmediaextractor site-a.com site-b.com      # several URLs -> one JSON object keyed by URL

The command prints JSON to stdout. It exits with 1 if any URL fails to fetch and 2 for invalid arguments (for example an unknown --platform).

How it works

flowchart TD
    U["extract_social_media_accounts(url)"] --> S["_select(platforms)<br/>validate names, X -> Twitter"]
    S --> F["fetch_html<br/>requests / Session, timeout,<br/>raise_for_status"]
    F -- "network or HTTP error" --> E["SocialMediaExtractorError"]
    F -- "(html, final_url)" --> H["extract_social_links_from_html"]
    RAW["HTML you already have"] --> H
    RAW --> A
    H --> A["extract_all_social_links"]
    A --> C["iter_candidate_urls (BeautifulSoup)<br/>1. JSON-LD sameAs<br/>2. link href<br/>3. a href"]
    C --> N["_normalize<br/>resolve relative and //links,<br/>drop mailto:, tel:, javascript:"]
    N --> M["Platform.matches (PLATFORMS table)<br/>host or subdomain match,<br/>excluded first segment,<br/>path regex fullmatch"]
    M --> D["_dedupe_key<br/>platform + path (case, trailing /)<br/>+ query without utm_*"]
    D --> R["dict[str, list[str]]<br/>every distinct profile"]
    R --> FIRST["first link per platform<br/>dict of str or None"]

Key design decisions:

  • Declarative rules. Each network is a frozen Platform(name, hosts, path, exclude_first_segment) in PLATFORMS. A URL is a profile only if its host is (a subdomain of) a listed host, its first path segment is not reserved (intent, sharer, login, ...), and its whole path matches the profile regex. Adding a network is a data change plus fixtures.
  • Order is meaning. Candidates are yielded JSON-LD first, then <link>, then <a>, so the "first link" is the one the site declared most deliberately.
  • Fail loudly on fetch, quietly on content. Network and HTTP errors raise SocialMediaExtractorError; broken or pathologically nested JSON-LD blocks are skipped (logged at DEBUG on the socialmediaextractor.extractor logger) so one bad block cannot abort a page.
  • No hidden state. No caching, globals or retries; pass your own requests.Session for connection reuse, proxies or retry adapters.

Use cases and recipes

All examples run offline: bundled sample pages are served through a fake session.

Script What it shows
examples/quickstart.py First link per platform and all links from a saved page; share buttons skipped
examples/enrich_leads_csv.py Add LinkedIn / X / Facebook / Instagram columns to a CSV of company websites (--live to fetch real sites)
examples/audit_brand_links.py Audit a site for missing networks, multiple accounts and legacy twitter.com links
examples/classify_url_list.py Label a list of URLs (backlinks, CRM fields) as profiles or not, without fetching

Configuration

There are no environment variables. Options are function arguments:

Option Applies to Default Effect
platforms all extractors all 9 Restrict and order the result; case-insensitive; X is an alias for Twitter; unknown names raise ValueError
base_url HTML extractors None Resolve relative links against this URL (fetching uses the final URL after redirects)
timeout fetch_html, extract_social_media_accounts, CLI --timeout 15.0 s Request timeout
session fetch_html, extract_social_media_accounts None A requests.Session to reuse (proxies, retries, cookies)
--all CLI off Print every distinct link per platform instead of the first

Requests send the headers in socialmediaextractor.extractor.DEFAULT_HEADERS (a descriptive User-Agent and Accept: text/html).

API overview

Name Returns
extract_social_media_accounts(url, *, platforms=None, timeout=15.0, session=None) Fetches url; dict[str, str | None], first profile link per platform
extract_social_links_from_html(html, base_url=None, *, platforms=None) Same, from an HTML string
extract_all_social_links(html, base_url=None, *, platforms=None) dict[str, list[str]], every distinct profile link in page order
classify_url(url) Platform name, or None if the URL is not a profile
iter_candidate_urls(html, base_url=None) Iterator over all absolute http(s) URLs in a page, in priority order
fetch_html(url, *, timeout=15.0, session=None) (html, final_url); raises SocialMediaExtractorError
PLATFORMS, Platform The matching rules (name, hosts, path regex, excluded segments)
SocialMediaExtractorError Raised when a page cannot be fetched (subclass of RuntimeError)
Upgrading from 0.x
  • from socialmediaextractor import extract_social_media_accounts works again. The 0.2 to 0.4 wheels on PyPI shipped without any Python code, so the import failed.
  • The Facebook, LinkedIn and Twitter keys are unchanged. Six new platform keys were added, and Twitter now also matches x.com.
  • Share buttons and other non-profile links are no longer returned.
  • Network and HTTP errors now raise SocialMediaExtractorError. Before, an error page was silently parsed.

References

The link sources follow these specifications:

Limitations

  • Only the HTML returned by the server is parsed; links injected by JavaScript are not seen. Pass the rendered HTML from a headless browser to the *_from_html functions if you need them.
  • Rules match URL shapes, not account existence: a matched URL may point to a deleted account.
  • The rules cover the URL formats in the test suite; platforms change their URL schemes over time.

Roadmap

  • More networks (for example Mastodon, Pinterest, Reddit, Discord invite links).
  • Optional crawl of a site's contact/about page when the home page has no profiles.
  • Async fetching for large batches.

Contributing, security, citation, license

Development setup
git clone https://github.com/irfanalidv/socialmediaextractor.git
cd socialmediaextractor
uv venv && uv pip install -e ".[dev]"
pre-commit install
ruff check . && ruff format --check .
mypy socialmediaextractor
pytest --cov=socialmediaextractor

The tests run offline against saved HTML fixtures in tests/fixtures/.

Metadata

Release files for socialmediaextractor 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for socialmediaextractor 1.1.0
File Size Uploaded Provenance Actions
socialmediaextractor-1.1.0.tar.gz 26.6 kB Without provenance Details for socialmediaextractor-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for socialmediaextractor 1.1.0
File Interpreter ABI Platform Actions
socialmediaextractor-1.1.0-py3-none-any.whl Python 3 none any Details for socialmediaextractor-1.1.0-py3-none-any.whl

Total release size: 40.5 kB

Release files / socialmediaextractor-1.1.0.tar.gz

Download URL socialmediaextractor-1.1.0.tar.gz
Size 26.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e2472291e46d854d42eb7a6fd69ea302d4302b1b03d3d59d316a69c7d1e7cfbf
BLAKE2b-256 checksum
How to use checksums
44b6e6459822925c2c4a19bb3e86bf09a6d4c38966c6c2a88b5ea9c683a9acc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.7

Release files / socialmediaextractor-1.1.0-py3-none-any.whl

Download URL socialmediaextractor-1.1.0-py3-none-any.whl
Size 13.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69599fd1f6f6a45374a348becf493e01856f558dc2243ae604c6289930025e8d
BLAKE2b-256 checksum
How to use checksums
6982d841bafc448bc42b64c764798091c2861d57a34a6ac09e165f3692a32a46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.7

Security for socialmediaextractor 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page