Saving an Article's Like and Share Counts to a Database

Short answer

How do you get like and share counts from an article into a database?

Fetch the rendered article page, because counters usually load with JavaScript, then select the like and share elements and convert values such as 1.2K into integers. Insert one row per fetch with the URL, counts and timestamp so you can chart change over time. AlterLab returns the rendered page ready for parsing.

Step by step

  1. 1

    Render the page

    Engagement counters are often filled in by scripts after load, so fetch the page with JavaScript rendering.

  2. 2

    Select the counters

    Find stable selectors or data attributes for the like and share elements and read their text.

  3. 3

    Normalise the numbers

    Convert abbreviations such as 1.2K or 3M into integers before storing them.

  4. 4

    Store snapshots

    Insert a row per fetch rather than updating one row, so you keep the history.

Example

Python
import sqlite3
from datetime import datetime, timezone

def to_int(text: str) -> int:
    text = text.strip().upper().replace(",", "")
    scale = {"K": 1_000, "M": 1_000_000}.get(text[-1:], 1)
    return int(float(text.rstrip("KM")) * scale)

db = sqlite3.connect("engagement.db")
db.execute("CREATE TABLE IF NOT EXISTS counts (url TEXT, likes INT, shares INT, at TEXT)")
db.execute(
    "INSERT INTO counts VALUES (?, ?, ?, ?)",
    ("https://example.com/article", to_int("1.2K"), to_int("85"),
     datetime.now(timezone.utc).isoformat()),
)
db.commit()

Frequently Asked Questions

Why are the counts missing from the HTML I downloaded?

They are probably loaded by JavaScript after the initial HTML. Fetch the rendered page, or find the JSON request the page makes for the counts.

How often should I record counts?

Hourly in the first days after publication, when counts change fastest, then daily.

Read the full guide

How to extract news articles

Try it with AlterLab

Send a URL, get the page back as HTML, markdown or JSON. Rendering, proxy rotation and retries are handled for you.

View API docs

Your first scrape.
Sixty seconds.

Up to 5,000 free requests — about 5,000 Light, 500 Protected or 250 Dynamic requests. No credit card.Just a POST request.

terminal
curl -X POST https://api.alterlab.io/v1/scrape \
-H "X-API-Key: YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com", "formats": ["markdown"]}'

No credit card required · Up to 5,000 free requests (about 5,000 Light, 500 Protected or 250 Dynamic requests) · Balance never expires