DEV Community

Bachir Djoukbala
Bachir Djoukbala

Posted on

How to extract emails and phone numbers from a list of websites with Python (no API key)

If you have a list of company websites and need contact emails, you can get a long way with a few lines of Python. Here is a minimal version that checks the homepage plus the usual contact pages.

import re, httpx
EM = re.compile(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}")
def find(url):
    out = set()
    for p in ["", "/contact", "/impressum"]:
        try:
            t = httpx.get(url.rstrip("/") + p, follow_redirects=True, timeout=10).text
        except Exception:
            continue
        out |= {e for e in EM.findall(t) if not e.lower().endswith(("png", "jpg", "gif", "webp"))}
    return out

print(find("https://www.heise.de"))   # {'newstipps@heise.de'}
Enter fullscreen mode Exit fullscreen mode

I tested this on heise.de and it returned a real newsroom address. It is fine for a handful of sites. Its limits: no phone parsing, no social links, no deduplication of obfuscated emails, and no concurrency or retries.

Doing it at scale

I built an Apify actor for this: Website Contact Finder (disclosure: I am the author). You give it a list of URLs and it returns emails, phone numbers and social profile links per website, priced pay-per-result at about $0.0007 per website.

https://apify.com/rejoicing_shanty/website-contact-finder-emails-phones

Feedback and edge cases welcome in the comments.

Top comments (0)