Package: web2preview
web2previewFor a given URLweb2previewextracts itstitle,description, andimage urlusingOpen Graph,Twitter Card, orSchemameta tags, or, as an alternative, parses it as a generic webpage.This is aforkof an excellentwebpreviewlibrary and it maintainscomplete and absolutecompatibility with the original while fixing several bugs, enhancing parsing, and adding a new
convenient APIs.Main differences betweenweb2previewandwebpreview:Enhanced parsing for generic web pagesNo unnecessaryGETrequest is ever made ifcontentof the page is suppliedComplete fallback mechanism which continues to parse until all methods are exhaustedPython Typings are added across the entire library (better syntax highlighting)New dict-likeWebPreviewresult object makes it easier to read parsing resultsCommand-line utility to extract title, description, and image from URLInstallationpipinstallweb2previewUsageUse the genericweb2previewmethod 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.>>>fromweb2previewimportweb2preview>>>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.urlTrue# 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 lineWhenweb2previewis installed viapipthe accompanying command-line tool is intalled alongside.$web2previewhttps://en.wikipedia.org/wiki/Enrico_Fermi
title:EnricoFermi-Wikipedia
description:Italian-Americanphysicist(1901–1954)image:https://upload.wikimedia.org/wikipedia/commons/thumb/d/d4/Enrico_Fermi_1943-49.jpg/1200px-Enrico_Fermi_1943-49.jpg

$web2previewhttps://github.com/--absolute-url
title:GitHub:Wheretheworldbuildssoftware
description:GitHubiswhereover83milliondevelopersshapethefutureofsoftware,together.
image:https://github.githubassets.com/images/modules/site/social-cards/github-social.pngNote: For the OriginalwebpreviewAPI please check theofficial docs.Run with DockerThe docker image can be built and ran similarly to the command line.
The default entry point is theweb2previewcommand-line function.$dockerbuild-tweb2preview.
$dockerrun-it--rmweb2preview"https://en.m.wikipedia.org/wiki/Enrico_Fermi"title:EnricoFermi-Wikipedia
description:EnricoFermi(Italian:[enˈriːkoˈfermi];29September1901–28November1954)wasanItalian(laternaturalizedAmerican)physicistandthecreatoroftheworld'sfirstnuclearreactor,theChicagoPile-1.Hehasbeencalledthe"architect of the nuclear age"[1]andthe"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.jpgNote: built docker image weighs around 210MB.Testing# Execute the testspoetryrunpytestweb2preview# OR execute until the first failed testpoetryrunpytestweb2preview-xSetting up development environment# Install a correct minimal supported version of pythonpyenvinstall3.7.13# Create a virtual environment# By default, the project already contains a .python-version file that points# to 3.7.13.python-mvenv.venv# Install dependencies# Poetry will automatically install them into the local .venvpoetryinstall# If you have errors likes this:ERROR:Cannotexecute`setup.py`sincesetuptoolsisnotavailableinthebuildenvironment.# Then do this:.venv/bin/pipinstall--upgradesetuptools