Linkpreview
raw JSON → 0.12.1 verified Fri May 01 auth: no python
Python library to extract link previews (title, description, image, etc.) from URLs. Version 0.12.1. No regular release cadence; final release was 2020.
pip install linkpreview Common errors
error AttributeError: module 'linkpreview' has no attribute 'get_preview' ↓
cause The function `get_preview` was renamed to `link_preview` in v0.10.0.
fix
Use
from linkpreview import link_preview and call link_preview(url). error linkpreview.exceptions.LinkPreviewException: No preview data found ↓
cause The URL could not be fetched or the page did not contain any meta tags for preview (e.g., no Open Graph, Twitter cards, or schema.org). Also occurs if the request is blocked.
fix
Ensure the URL is accessible (set a User-Agent if needed), or handle the exception gracefully.
error TypeError: __init__() got an unexpected keyword argument 'headers' ↓
cause The `LinkPreview` class in older versions (<0.11) did not accept `headers` directly. The functional API `link_preview` also does not accept headers.
fix
Upgrade to the latest version (0.12.1). For the class-based API, use
LinkPreview(url, headers={'User-Agent': '...'}). For the functional API, consider using requests.Session to set headers globally. Warnings
breaking In v0.10.0, the function `get_preview` was renamed to `link_preview`. Code using `get_preview` will raise AttributeError. ↓
fix Replace `get_preview(url)` with `link_preview(url)`.
deprecated The `LinkPreview` class constructor changed: in older versions, you passed a URL directly; in v0.11+, you must pass a string and then call `fetch_and_extract()`. ↓
fix Use the functional API `link_preview(url)` for simplicity, or update class usage: `preview = LinkPreview(url); preview.fetch_and_extract()`.
gotcha The library does not set a User-Agent header by default. Some websites may block requests, returning empty or malformed previews. ↓
fix Pass a custom headers dictionary when using the class-based API: `LinkPreview(url, headers={'User-Agent': 'Mozilla/5.0'})` or use requests.Session.
gotcha The library does not handle non-HTTP/HTTPS schemes (e.g., ftp://, file://). Attempting to parse such URLs may raise an exception or fail silently. ↓
fix Validate the URL scheme before passing to linkpreview; skip unsupported schemes.
Imports
- link_preview wrong
from linkpreview import get_previewcorrectfrom linkpreview import link_preview - LinkPreview
from linkpreview import LinkPreview
Quickstart
from linkpreview import link_preview
url = "https://example.com"
preview = link_preview(url)
print(preview.title)
print(preview.description)
print(preview.image)
print(preview.force_title)
print(preview.force_description)
print(preview.force_image)