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)
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)
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)
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)
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.7.0.tar.gz
(9.1 kB
view details)
Built Distribution
File details
Details for the file linkpreview-0.7.0.tar.gz
.
File metadata
- Download URL: linkpreview-0.7.0.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99569a257668d4ace81a9b55f76830ed74370f72617674fe3b44e210f6fb3063 |
|
MD5 | 2544f3a4b9228c934f43cf0f7507bcfd |
|
BLAKE2b-256 | c6f22c2b53d9458d1a9ccdd9bb87cdd0fcb74ef730b6e3629077310ea01cfba0 |
File details
Details for the file linkpreview-0.7.0-py3-none-any.whl
.
File metadata
- Download URL: linkpreview-0.7.0-py3-none-any.whl
- Upload date:
- Size: 11.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b6c00753e861acfad5495daa352a494b86e6f93a5533b1520b02219fb597257e |
|
MD5 | 8b957ac894547a94ba68e7c492e985b4 |
|
BLAKE2b-256 | e6f7756d82c9fd0714a284b71c8d7c089a44ec60f5d1cb6abe9b13257f81b182 |