SocLeads
SocLeads Journal

Python Email Scraper Tutorial: A Tested HTML-to-CSV Example

May 15, 2026 4 min read CHRIS JOHNSON, CUSTOMER SUCCESS AT SOCLEADS.COM
Run a small Python email extractor on a local HTML fixture, inspect the expected CSV and understand its limits before using a permitted web page.

This tutorial builds a small extractor for one HTML document. It reads text and mailto links, removes duplicate candidate addresses and writes their source to CSV. The included fixture is synthetic, so you can verify the result without collecting anyone’s personal information.

Prepare the local fixture

Use Python 3.10 or later. The example uses only the standard library. Save the following as fixture.html; example.test is a placeholder domain used for demonstration.

<!doctype html><html><body>
<p>Contact [email protected]</p>
<a href="mailto:team%40example.test?subject=Hello">Team</a>
<p>Duplicate: [email protected]</p>
<script>const hidden = "[email protected]";</script>
</body></html>

Save and run the extractor

Save this complete example as email_scraper_example.py. It deliberately processes one document and does not follow links or execute JavaScript.

"""Extract published-looking emails from one local HTML fixture or permitted URL.

Usage: python email_scraper_example.py fixture.html contacts.csv
This example does not validate delivery, infer addresses or crawl linked pages.
"""
import csv
import re
import sys
from html.parser import HTMLParser
from pathlib import Path
from urllib.parse import unquote, urlsplit
from urllib.request import Request, urlopen

EMAIL = re.compile(r"[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+")
LIMIT = 1_000_000


class Contacts(HTMLParser):
    def __init__(self):
        super().__init__(convert_charrefs=True)
        self.ignored = 0
        self.emails = set()

    def collect(self, text):
        self.emails.update(value.rstrip('.,;:!?').lower() for value in EMAIL.findall(text))

    def handle_starttag(self, tag, attrs):
        if tag in ('script', 'style'):
            self.ignored += 1
        if not self.ignored and tag == 'a':
            href = dict(attrs).get('href', '')
            if href.lower().startswith('mailto:'):
                self.collect(unquote(href[7:].split('?', 1)[0]))

    def handle_endtag(self, tag):
        if tag in ('script', 'style'):
            self.ignored = max(0, self.ignored - 1)

    def handle_data(self, text):
        if not self.ignored:
            self.collect(text)


def load(source):
    parts = urlsplit(source)
    if parts.scheme in ('http', 'https'):
        request = Request(source, headers={'User-Agent': 'ContactResearchExample/1.0'})
        with urlopen(request, timeout=10) as response:
            if response.headers.get_content_type() != 'text/html':
                raise ValueError('Expected an HTML page')
            data = response.read(LIMIT + 1)
            charset = response.headers.get_content_charset() or 'utf-8'
    else:
        data = Path(source).read_bytes()
        charset = 'utf-8'
    if len(data) > LIMIT:
        raise ValueError('Example input is too large')
    return data.decode(charset, errors='replace')


def main():
    if len(sys.argv) != 3:
        raise SystemExit('Usage: python email_scraper_example.py INPUT OUTPUT.csv')
    source, output = sys.argv[1:]
    parser = Contacts()
    parser.feed(load(source))
    parser.close()
    with open(output, 'w', newline='', encoding='utf-8') as stream:
        writer = csv.writer(stream)
        writer.writerow(['email', 'source'])
        writer.writerows((email, source) for email in sorted(parser.emails))
    print(f'Wrote {len(parser.emails)} unique candidate addresses to {output}')


if __name__ == '__main__':
    main()
python email_scraper_example.py fixture.html contacts.csv

Inspect the expected result

The fixture produces two unique candidate addresses. The duplicate is normalised; the address inside the script element is ignored. This exact local fixture was tested with the published example.

email,source
[email protected],fixture.html
[email protected],fixture.html

The output describes text found in the input. It does not establish that a mailbox exists, accepts messages or may be used for outreach. The regular expression is intentionally a practical example, not a complete implementation of every valid email syntax.

Use an actual web page only after checking the source

The same script can read one HTTP or HTTPS URL. Use it only for a page you own or are authorised to process, after checking applicable access rules and robots.txt. The script does not check robots.txt itself, authenticate to a website, evade access restrictions or crawl a whole domain. It uses a request timeout and a one-megabyte input limit.

Static HTML can differ from the content a browser displays after JavaScript runs. Hidden HTML text, image-based addresses, obfuscation and malformed markup need additional handling and manual review. Keep the source and review date, and avoid collecting unrelated fields.

Extend through tests before adding more pages

  • Add fixtures for missing addresses, encoded mailto links and duplicates.
  • Keep allowed domains and a maximum page count if you later build a crawler.
  • Handle request failures and redirects explicitly.
  • Retain source context and apply suppression records before use.

The implementation uses Python’s HTMLParser and urllib.request. For a source-selection overview, read the website extraction guide. For supported maps and social audience workflows, see SocLeads Email Finder.

Turn this research into a lead list

Collect verified emails from Google Maps, Instagram, Facebook and LinkedIn — free to start.

Start free with SocLeads →

Cookie preferences

Choose whether optional analytics can run. You can change this choice at any time.

Essential

Needed for sign-in, security and remembering your privacy choice.

Always active

Advertising cookies and personalization are not enabled.

Privacy policy