October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Blank Link Previews? Find a Missing og:image with Python’s Standard Library

A dependency-free Python script can identify whether the HTML returned by a URL contains a usable og:image tag. The lasting repair is in the page’s generated head markup.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If a link preview is blank because the page lacks an og:image tag, the lasting fix is to add that metadata to the page’s generated HTML. A small Python script using only the standard library can check the HTML returned by a URL and flag a missing or empty image value. It diagnoses the page markup; it does not change the site or refresh a platform’s cache.

What an og:image tag does

Open Graph defines four required properties for a page: og:title, og:type, og:image, and og:url. The og:image value is the URL of the image representing the page’s object. Add it as a meta element in the document’s <head>, using an image URL the intended preview consumer can retrieve. See the Open Graph protocol.

As an Amazon Associate I earn from qualifying purchases.

<head>
  <meta property="og:title" content="A useful page title">
  <meta property="og:type" content="website">
  <meta property="og:url" content="https://example.com/article">
  <meta property="og:image" content="https://example.com/images/article-preview.jpg">
  <meta property="og:image:alt" content="A description of the preview image">
</head>

The example image address is illustrative; replace it with a real image URL for your page. Open Graph recommends og:image:alt when an image is specified. Use a useful description of what the image depicts, not merely a caption.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check the returned HTML with Python

This diagnostic fetches a page, checks that the response is successful and HTML, decodes the response using its declared charset when available, and scans <meta> start tags for property="og:image". It reports an absent or empty value. It uses Python’s built-in urllib.request and html.parser, so it needs no third-party packages.

from html.parser import HTMLParser
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen


class OpenGraphImageFinder(HTMLParser):
    def __init__(self):
        super().__init__()
        self.images = []

    def handle_starttag(self, tag, attrs):
        if tag.lower() != "meta":
            return

        values = {key.lower(): value for key, value in attrs if key}
        if values.get("property", "").lower() == "og:image":
            self.images.append(values.get("content") or "")


def check_og_image(url):
    request = Request(url, headers={"User-Agent": "og-image-check/1.0"})

    try:
        with urlopen(request, timeout=15) as response:
            status = response.status
            content_type = response.headers.get_content_type()
            charset = response.headers.get_content_charset()
            html_bytes = response.read()
    except (HTTPError, URLError, TimeoutError) as error:
        print(f"Could not fetch page: {error}")
        return

    if not 200 <= status < 300:
        print(f"Unexpected HTTP status: {status}")
        return

    if content_type != "text/html":
        print(f"Expected HTML, got {content_type}")
        return

    # Use the response's declared charset, falling back to UTF-8.
    html = html_bytes.decode(charset or "utf-8", errors="replace")
    parser = OpenGraphImageFinder()
    parser.feed(html)

    usable_images = [image.strip() for image in parser.images if image.strip()]
    if usable_images:
        print("Found og:image:", usable_images[0])
    elif parser.images:
        print("og:image exists, but its content is empty.")
    else:
        print("No og:image meta tag found in the returned HTML.")


check_og_image("https://example.com/article")

Replace the final URL with the page you want to inspect. Python’s urllib.request documentation describes fetching URLs and reading response data as bytes. Its html.parser documentation explains how parser callbacks expose start-tag attributes; the parser can handle invalid HTML, but it is not a browser and does not promise browser-style recovery in every case.

Read the result correctly

  • “No og:image meta tag found” means no matching property appeared in the HTML fetched by this script. Add the metadata to the template or server-side output that generates the page.
  • “content is empty” means the tag exists but its content is blank or whitespace. Set its content to a usable image URL.
  • “Found og:image” means the script found a non-empty value in the returned markup. It does not establish that a social platform can retrieve, accept, or display that image.
  • Fetch or content-type errors mean the script did not inspect the intended HTML page. Check the URL, response, and server behavior before interpreting the metadata result.

Fix the page, not just the diagnostic

The script only reads the response. Once you find the source template, add or correct the og:image element in the page’s generated <head>, then fetch the page again and check the returned HTML. If the site renders its metadata through JavaScript after the initial response, this script may not see it; the diagnostic checks the HTML returned by the server, not a browser-rendered page.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Keep image alternatives and metadata in order

Open Graph permits multiple og:image properties. When values conflict, the first image tag in document order receives preference. Keep intentional alternatives rather than collapsing them blindly. Structured image properties—such as dimensions and alt text—belong after the corresponding og:image root and before the next image root begins. The protocol describes these rules at ogp.me.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

After correcting the HTML, a previously shared URL may still show an old preview: adding a tag does not guarantee an immediate cache update. Cache refresh behavior and platform-specific image requirements vary and are not determined by this Python check.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.