Get link (URL) preview
Project description
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)
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
linkpreview-0.10.0.tar.gz
(14.8 kB
view details)
Built Distribution
File details
Details for the file linkpreview-0.10.0.tar.gz
.
File metadata
- Download URL: linkpreview-0.10.0.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc871195fef47b9446ead5a693f40d4c022ed58ee226d39aa158705765e4e49c |
|
MD5 | d4dc84252fed1f19c94bed7d744200c4 |
|
BLAKE2b-256 | 3a50494d8fa005135e64762a5c3bde9593c3c7074c350004a3c56a92c69444d1 |
File details
Details for the file linkpreview-0.10.0-py3-none-any.whl
.
File metadata
- Download URL: linkpreview-0.10.0-py3-none-any.whl
- Upload date:
- Size: 20.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48de0419af89adbd208511fec024937dd80dee9d15237757eb69523c6cae162b |
|
MD5 | f352fece288249992dd5514db7b163db |
|
BLAKE2b-256 | 73ec258503d5d9cd75288420a98b7e9d2ab8bbffa941b708a487e0115eb3d9fd |