Short answer
To push scraped records into a CRM safely, normalize each record, build a stable dedupe key (such as a lowercase email or a phone number in E.164 format), look the key up in the CRM, and then upsert: update the record if it exists, create it if it doesn't. Wrap the calls in retries with backoff and log every result so a rerun never creates duplicates.
Why do duplicates happen?
Scrapers run repeatedly. If each run simply POSTs new contacts, you get the same company three times with slightly different spelling. The fix is to make the import idempotent: running it twice should give the same CRM state as running it once.
Step 1: Normalize before you compare
import re def norm_email(v: str | None) -> str | None: v = (v or "").strip().lower() return v if re.fullmatch(r"[^@\s]+@[^@\s]+\.[^@\s]+", v) else None def norm_phone(v: str | None) -> str | None: digits = re.sub(r"\D", "", v or "") if len(digits) == 10: digits = "1" + digits # assumes US numbers; adjust for your data return "+" + digits if len(digits) >= 11 else None def dedupe_key(rec: dict) -> str | None: return norm_email(rec.get("email")) or norm_phone(rec.get("phone")) Step 2: Dedupe inside the batch
def dedupe_batch(records): seen = {} for rec in records: key = dedupe_key(rec) if key is None: continue # route to a review file instead of the CRM seen.setdefault(key, {}).update({k: v for k, v in rec.items() if v}) return seen Step 3: Upsert against the CRM API
Most CRMs expose a search endpoint and create/update endpoints. The exact URLs and auth differ per product, so treat the base URL below as a placeholder.
import time, httpx BASE = "https://crm.example.com/api" # placeholder HEADERS = {"Authorization": "Bearer YOUR_TOKEN"} def request(client, method, url, **kw): for attempt in range(5): r = client.request(method, url, headers=HEADERS, timeout=20, **kw) if r.status_code in (429, 500, 502, 503, 504): time.sleep(2 ** attempt) continue r.raise_for_status() return r raise RuntimeError(f"gave up on {url}") def upsert(client, key, rec): found = request(client, "GET", f"{BASE}/contacts", params={"q": key}).json() if found: request(client, "PATCH", f"{BASE}/contacts/{found[0]['id']}", json=rec) return "updated" request(client, "POST", f"{BASE}/contacts", json=rec) return "created" def run(records): stats = {"created": 0, "updated": 0, "failed": 0} with httpx.Client() as client: for key, rec in dedupe_batch(records).items(): try: stats[upsert(client, key, rec)] += 1 except Exception as e: stats["failed"] += 1 print("failed", key, e) return stats Step 4: Make reruns safe
- Keep a local SQLite table of keys already synced with timestamps, so you can skip unchanged records.
- Never overwrite a CRM field with an empty value; the batch merge above only copies non-empty values.
- Send records without a valid key to a CSV for human review rather than guessing.
- Respect the source site's terms and the rules for storing personal data (consent, opt-outs) before importing contacts.
FAQ
What is the best dedupe key for scraped leads?
A normalized email if you have one, otherwise an E.164 phone number. For companies without either, a normalized domain name works.
Should I use bulk endpoints?
Yes when the CRM offers them, as they cut request counts. Keep the same lookup-then-upsert logic or use the CRM's native upsert-by-key feature if it has one.
How do I handle rate limits?
Retry on HTTP 429 and 5xx with exponential backoff, as in the request helper, and reduce batch concurrency.
How do I test this without touching production data?
Point BASE at a sandbox account and run the same batch twice; the second run should report zero created records.
I'm Muhammad Afzal, a Python developer. I build scraping and automation pipelines through INDUS Web Agency.


