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.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

If a link preview is blank because the page has no usable og:image, add an image URL to the page’s HTML head. The Python standard library can check the HTML your server returns and report whether the tag is present; it cannot add the metadata to your site or refresh a platform’s cache. Open Graph lists og:image among its four required properties. (Open Graph protocol)

What to add to the page

Place an explicit Open Graph image tag in the page’s <head>. Its content value should be a real image URL that the intended preview consumer can retrieve.

<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 URLs are illustrative; replace them with the page’s actual URL and an image hosted for your site. Open Graph’s four required properties are og:title, og:type, og:image, and og:url. When an image is specified, the protocol recommends og:image:alt to describe what it depicts. (Open Graph protocol)

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

Check the HTML your server returns with Python

This dependency-free diagnostic fetches a page, checks that the response looks like HTML, decodes its bytes using the response charset when one is declared, and lists its non-empty og:image values. It checks the returned HTML only; it does not emulate a browser or determine how a social platform crawls, validates, or caches the image.

from html.parser import HTMLParser
from urllib.request import 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 "").strip())


def check_og_image(page_url):
    with urlopen(page_url, timeout=15) as response:
        status = response.status
        content_type = response.headers.get_content_type()
        charset = response.headers.get_content_charset() or "utf-8"
        html_bytes = response.read()

    if not 200 <= status < 300:
        raise RuntimeError(f"Page returned HTTP status {status}")
    if content_type != "text/html":
        raise RuntimeError(f"Expected HTML, got {content_type}")

    html = html_bytes.decode(charset, errors="replace")
    parser = OpenGraphImageFinder()
    parser.feed(html)

    images = [image for image in parser.images if image]
    if images:
        print("Found og:image value(s):")
        for image in images:
            print(f"- {image}")
    elif parser.images:
        print("og:image tag found, but its content is empty.")
    else:
        print("No og:image tag found in the returned HTML.")


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

Save this as a Python file and replace the final URL with the page you want to inspect. The script uses only urllib.request and html.parser, both part of Python’s standard library. (Python urllib.request documentation; Python html.parser documentation)

Read the result correctly

  • “No og:image tag found”: The fetched HTML contains no matching meta tag. Add it to the template or page source that generates the response.
  • “Content is empty”: A matching tag exists, but it has no usable image URL. Supply a non-empty content value.
  • One or more values printed: The returned HTML declares image URL(s). That does not by itself verify that the image can be fetched or that a particular platform will display it.
  • An HTTP or content-type error: Check that the URL resolves to the intended page and returns HTML successfully; redirects, errors, or a non-HTML response can make the body unsuitable for this check.

Fix the source HTML, not the checker

If the tag is absent, update the page’s server-rendered HTML or the template that produces it, then fetch the page again. This script inspects the response body, not a browser’s rendered DOM. If your site adds Open Graph tags only with client-side JavaScript, this check may not see them in the initial response; make sure the HTML delivered to the relevant consumer includes the metadata.

The Python documentation notes that urlopen() returns response data as bytes and that the byte-stream encoding cannot generally be determined automatically. The example uses the response’s declared charset when available and falls back to UTF-8; if your site uses a different encoding without declaring it, adjust decoding to match the site. (Python urllib.request documentation)

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

Keep multiple image declarations in order

Open Graph permits multiple og:image properties. If you intentionally declare alternatives, preserve them rather than replacing them with a single value: when values conflict, the first image tag in document order receives preference. Structured image properties, including dimensions and alt text, should follow the corresponding og:image root tag before the next image root begins. (Open Graph protocol)

<meta property="og:image" content="https://example.com/images/primary.jpg">
<meta property="og:image:alt" content="The article’s main illustration">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
<meta property="og:image" content="https://example.com/images/alternative.jpg">
<meta property="og:image:alt" content="An alternate article image">
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What this Python check cannot fix

Finding a tag confirms only that the returned HTML contains a non-empty og:image value. It does not fetch or validate the image file, establish platform-specific image requirements, or force an already shared link to update. Once the site’s HTML is corrected, any refresh or cache behavior depends on the platform consuming the URL.

Python’s HTMLParser exposes attributes from start tags and can parse invalid HTML, making it suitable for a small metadata check. It is not a browser, and its documentation does not promise browser-equivalent recovery behavior. (Python html.parser documentation)

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.

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