linkpreview
Get link preview in python
Gathering data from:
- OpenGraph meta tags
- TwitterCard meta tags
- Microdata meta tags
- JSON-LD meta tags
- HTML Generic tags (
h1,p,img) - URL readable parts
Install
pip install linkpreview
Usage
Basic
from linkpreview import link_preview
url = "http://localhost"
content = """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<!-- ... --->
<title>a title</title>
</head>
<body>
<!-- ... --->
</body>
</html>
"""
preview = link_preview(url, content)
print("title:", preview.title)
print("description:", preview.description)
print("image:", preview.image)
print("force_title:", preview.force_title)
print("absolute_image:", preview.absolute_image)
print("site_name:", preview.site_name)
print("favicon:", preview.favicon)
print("absolute_favicon:", preview.absolute_favicon)
Automatic fetch link content
from linkpreview import link_preview
preview = link_preview("http://github.com/")
print("title:", preview.title)
print("description:", preview.description)
print("image:", preview.image)
print("force_title:", preview.force_title)
print("absolute_image:", preview.absolute_image)
print("site_name:", preview.site_name)
print("favicon:", preview.favicon)
print("absolute_favicon:", preview.absolute_favicon)
lxml as XML parser
Very recommended for better performance.
Install the lxml and use it like this:
from linkpreview import link_preview
preview = link_preview("http://github.com/", parser="lxml")
print("title:", preview.title)
print("description:", preview.description)
print("image:", preview.image)
print("force_title:", preview.force_title)
print("absolute_image:", preview.absolute_image)
print("site_name:", preview.site_name)
print("favicon:", preview.favicon)
print("absolute_favicon:", preview.absolute_favicon)
Security
If URLs come from untrusted users, pass allow_private=False to avoid
SSRF: only http/https URLs resolving to public addresses are fetched
(re-checked on every redirect), otherwise BlockedAddressError is raised.
link_preview(url, allow_private=False)
Advanced
from linkpreview import Link, LinkPreview, LinkGrabber
url = "http://github.com"
grabber = LinkGrabber(
initial_timeout=20,
maxsize=1048576,
receive_timeout=10,
chunk_size=1024,
)
content, url = grabber.get_content(url)
link = Link(url, content)
preview = LinkPreview(link, parser="lxml")
print("title:", preview.title)
print("description:", preview.description)
print("image:", preview.image)
print("force_title:", preview.force_title)
print("absolute_image:", preview.absolute_image)
print("site_name:", preview.site_name)
print("favicon:", preview.favicon)
print("absolute_favicon:", preview.absolute_favicon)
Extend default headers:
content, url = grabber.get_content(url, headers={'user-agent': 'Twitterbot'})
Ignore default headers:
content, url = grabber.get_content(
url,
headers={'user-agent': 'Twitterbot', 'accept': '*/*'},
replace_headers=True,
)
Use preset headers:
content, url = grabber.get_content( url, headers='googlebot')
Available presets:
firefox,
chrome,
googlebot,
twitterbot,
telegrambot,
imessagebot
If you already have parsed BeautifulSoup object:
from bs4 import BeautifulSoup
from linkpreview import Link, LinkPreview
url = "http://example.com"
content = "<h1>Hello</h1>"
soup = BeautifulSoup(content, "html.parser")
link = Link(url, content)
preview = LinkPreview(link, soup=soup)
print("title:", preview.title)
Metadata
Release files for linkpreview 0.13.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| linkpreview-0.13.3.tar.gz | 17.0 kB | Without provenance | Details for linkpreview-0.13.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| linkpreview-0.13.3-py3-none-any.whl | Python 3 | none | any | Details for linkpreview-0.13.3-py3-none-any.whl |
Total release size: 40.2 kB
Release files / linkpreview-0.13.3.tar.gz
| Download URL | linkpreview-0.13.3.tar.gz |
|---|---|
| Size | 17.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0e0270f929bfa52ddca43ed86417fbe73875af639ada4b36af0842c67fcd194b
|
|
BLAKE2b-256 checksum How to use checksums |
b3d394fae369f5519e88ed534076f9fb0f404968ec61fdd5e1647d90c3a39fb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / linkpreview-0.13.3-py3-none-any.whl
| Download URL | linkpreview-0.13.3-py3-none-any.whl |
|---|---|
| Size | 23.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1d4508e9a08b9c1093ee2629a04119c4d4a74aa905d2cecf09eb591d57a63152
|
|
BLAKE2b-256 checksum How to use checksums |
76f587e3bc8088ac137aba81e3294a509dde4c14b1a277ffcb47c6c377427093
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for linkpreview 0.13.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.