Skip to main content

web2preview

For a given URL web2preview extracts its title, description, and image url using Open Graph, Twitter Card, or Schema meta tags, or, as an alternative, parses it as a generic webpage.

PyPI - Python Version PyPI Build status Code coverage report

This is a fork of an excellent webpreview library and it maintains complete and absolute compatibility with the original while fixing several bugs, enhancing parsing, and adding a new convenient APIs.

Main differences between web2preview and webpreview:

  • Enhanced parsing for generic web pages
  • No unnecessary GET request is ever made if content of the page is supplied
  • Complete fallback mechanism which continues to parse until all methods are exhausted
  • Python Typings are added across the entire library (better syntax highlighting)
  • New dict-like WebPreview result object makes it easier to read parsing results
  • Command-line utility to extract title, description, and image from URL

Installation

pip install web2preview

Usage

Use the generic web2preview method to parse the page independent of its nature. It tries to extract the values from Open Graph properties, then it falls back to Twitter Card format, then Schema. If none of them can extract all three of the title, description, and preview image, then webpage's content is parsed using a generic extractor.

>>> from web2preview import web2preview

>>> p = web2preview("https://en.wikipedia.org/wiki/Enrico_Fermi")
>>> p.title
'Enrico Fermi - Wikipedia'
>>> p.description
'Italian-American physicist (1901–1954)'
>>> p.image
'https://upload.wikimedia.org/wikipedia/commons/thumb/d/d4/Enrico_Fermi_1943-49.jpg/1200px-Enrico_Fermi_1943-49.jpg'

# Access the parsed fields both as attributes and items
>>> p["url"] == p.url
True

# Check if all three of the title, description, and image are in the parsing result
>>> p.is_complete()
True

# Provide page content from somewhere else
>>> content = """
<html>
    <head>
        <title>The Dormouse's story</title>
        <meta property="og:description" content="A Mad Tea-Party story" />
    </head>
    <body>
        <p class="title"><b>The Dormouse's story</b></p>
        <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>
    </body>
</html>
"""

# This function call won't make any external calls,
# only relying on the supplied content, unlike the example above
>>> web2preview("aa.com", content=content)
WebPreview(url="http://aa.com", title="The Dormouse's story", description="A Mad Tea-Party story")

Using the command line

When web2preview is installed via pip the accompanying command-line tool is intalled alongside.

$ web2preview https://en.wikipedia.org/wiki/Enrico_Fermi
title: Enrico Fermi - Wikipedia
description: Italian-American physicist (1901–1954)
image: https://upload.wikimedia.org/wikipedia/commons/thumb/d/d4/Enrico_Fermi_1943-49.jpg/1200px-Enrico_Fermi_1943-49.jpg

$ web2preview https://github.com/ --absolute-url
title: GitHub: Where the world builds software
description: GitHub is where over 83 million developers shape the future of software, together.
image: https://github.githubassets.com/images/modules/site/social-cards/github-social.png

Note: For the Original webpreview API please check the official docs.

Run with Docker

The docker image can be built and ran similarly to the command line. The default entry point is the web2preview command-line function.

$ docker build -t web2preview .
$ docker run -it --rm web2preview "https://en.m.wikipedia.org/wiki/Enrico_Fermi"
title: Enrico Fermi - Wikipedia
description: Enrico Fermi (Italian: [enˈriːko ˈfermi]; 29 September 1901 – 28 November 1954) was an Italian (later naturalized American) physicist and the creator of the world's first nuclear reactor, the Chicago Pile-1. He has been called the "architect of the nuclear age"[1] and the "architect of the atomic bomb".
image: https://upload.wikimedia.org/wikipedia/commons/thumb/d/d4/Enrico_Fermi_1943-49.jpg/1200px-Enrico_Fermi_1943-49.jpg

Note: built docker image weighs around 210MB.

Testing

# Execute the tests
poetry run pytest web2preview

# OR execute until the first failed test
poetry run pytest web2preview -x

Setting up development environment

# Install a correct minimal supported version of python
pyenv install 3.7.13

# Create a virtual environment
# By default, the project already contains a .python-version file that points
# to 3.7.13.
python -m venv .venv

# Install dependencies
# Poetry will automatically install them into the local .venv
poetry install

# If you have errors likes this:
ERROR: Can not execute `setup.py` since setuptools is not available in the build environment.

# Then do this:
.venv/bin/pip install --upgrade setuptools

Metadata

Release files for web2preview 1.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for web2preview 1.1.3
File Size Uploaded
web2preview-1.1.3.tar.gz 15.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for web2preview 1.1.3
File Interpreter ABI Platform
web2preview-1.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 34.1 kB

Release files / web2preview-1.1.3.tar.gz

Download URL web2preview-1.1.3.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
89c4a9e3421a35350dbdec399bdf55c397c3ec2de8bb0807520596b3974461ec
BLAKE2b-256 checksum
How to use checksums
251f3939a48ddd28ea09008d8fc191e4e4413a8e8dd775f857041ab42ac6fd38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.2 Darwin/21.5.0

Release files / web2preview-1.1.3-py3-none-any.whl

Download URL web2preview-1.1.3-py3-none-any.whl
Size 18.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6c67e2e1c6c78224937a68b30afa829f1fc4b785f8f2f9cd6343ffcf89cfab53
BLAKE2b-256 checksum
How to use checksums
88b61d4b347d4d431a3da96b31f5fa783681c502b12ebbb388259d5c11b735c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.2 Darwin/21.5.0

Release history Release notifications | RSS feed

This release

1.1.3 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page