Automating Contact Extraction Using Python, Google Search, and OpenAI

Combining Google search results, web scraping, and an LLM turns manual lead-research work — finding a company’s contact info one search at a time — into a repeatable script. Here’s the actual pipeline, with the compliance caveat that matters most.

The Pipeline

  1. Search — query a search API (not scraping Google’s results page directly, which violates its terms of service) for the target company or person, using a service like Google Custom Search API or a third-party search API.
  2. Fetch and extract — pull the top relevant page(s) and extract the raw text content.
  3. Structure with an LLM — feed the extracted text to an LLM with a prompt asking it to extract contact information into structured JSON, which handles the variation in how contact info appears across different site layouts far better than regex alone.
from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY")

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{
        "role": "user",
        "content": f"Extract any contact information (email, phone, name, title) "
                    f"from this text as JSON. Return null for fields not found:\n\n{page_text}"
    }],
    response_format={"type": "json_object"}
)
contact_data = response.choices[0].message.content

Why LLM Extraction Beats Pure Regex

Regex-based extraction breaks constantly against real-world page variation — different formatting, obfuscated emails, contact info embedded in unpredictable page structures. An LLM handles this variation naturally since it’s working from understanding, not pattern-matching a fixed format, at the cost of being slower and more expensive per request than a regex pass.

The Compliance Issue That Actually Matters

Scraping publicly available contact information is legally different from how you’re permitted to use it — many jurisdictions have specific regulations (GDPR in the EU, CAN-SPAM and similar laws elsewhere) governing unsolicited contact and data collection, regardless of whether the information was technically public. Building the extraction pipeline is the easy part; using the output compliantly (proper consent basis, opt-out mechanisms, respecting robots.txt and terms of service on source sites) is the part that carries real legal risk if skipped.

Practical Safeguards

  • Respect robots.txt and each source site’s terms of service — many explicitly prohibit scraping.
  • Rate-limit requests to avoid overloading source sites or triggering IP blocks.
  • Build a clear data retention and deletion policy for extracted contact data, not just a collection pipeline.

Frequently Asked Questions

Is this legal to do at all?
Extracting publicly available data is generally legal in many contexts, but how you subsequently use it (unsolicited outreach specifically) is governed by separate regulations that vary by jurisdiction — consult applicable law for your specific use case rather than assuming “it’s public” settles the question.

Conclusion

Combining search, scraping, and LLM-based extraction turns manual contact research into a scriptable pipeline, with LLM extraction meaningfully outperforming regex for handling real-world page variation. The technical pipeline is straightforward; using the output within applicable data and communication regulations is the part requiring actual care.

📑 About the author: I also build Digital Bizz Card — hosted digital business cards you can share with a QR code, no app required.

Translate »
Scroll to Top