extruct
repository·master·Indexed 21 days ago
https://github.com/scrapinghub/extructA specialized Python library for extracting embedded metadata from HTML documents. It supports multiple formats including JSON-LD, W3C HTML Microdata, Open Graph, Microformats (via mf2py), RDFa (experimental via rdflib), and Dublin Core Metadata (DC-HTML-2003). The library provides a high-level extract() method for all-in-one extraction, individual extractor classes for specific syntaxes, and a command-line tool for fetching and processing URLs.
What's inside extruct
- extruct is a Python library designed to extract embedded metadata from HTML markup. It supports several common metadata formats, allowing you to programmatically access structured data embedded within web pages.
Supported metadata formats in extruct
masterextruct provides extraction capabilities for the following formats:
- W3C's HTML Microdata
- embedded JSON-LD
- Microformat (via
mf2py) - Facebook's Open Graph
- RDFa (experimental, via
rdflib) - Dublin Core Metadata (DC-HTML-2003)
Install the extruct command line tool
masterThe
extructcommand line tool requires therequestslibrary, which is not included in the base installation. To installextructwith the necessary dependencies for the CLI, use thecliextra requirement:pip install 'extruct[cli]'Install extruct via pip
masterYou can install the extruct library using
pip.pip install extructUse the extruct CLI to extract metadata
masterThe
extructcommand line tool fetches a URL and outputs extracted metadata (Microdata, JSON-LD, RDFa, Open Graph, and Microformat) directly tostdout.To extract all supported metadata formats from a page:
extruct "http://example.com"Extract Dublin Core metadata using DublinCoreExtractor
masterUse
extruct.dublincore.DublinCoreExtractorto extract Dublin Core metadata from HTML<meta>and<link>elements. Theextract(html)method returns a list containing a dictionary withelements(individual metadata items),namespaces, andterms. Each element inelementsincludes theURI,content,name, and optionallangorscheme.from extruct.dublincore import DublinCoreExtractor html = '<meta name="DC.title" content="Example Title" />'<br> dublinlde = DublinCoreExtractor() data = dublinlde.extract(html)Extract Microformats using MicroformatExtractor
masterUse
extruct.microformat.MicroformatExtractorto extract Microformats from HTML. Theextract(html)method returns a list of dictionaries containing thetype(e.g.,h-entry) and apropertiesdictionary. Properties can contain simple string values or nested dictionaries for complex types (likeh-card).from extruct.microformat import MicroformatExtractor html = '<article class="h-entry"><h1 class="p-name">Title</h1></article>'<br> microformate = MicroformatExtractor() data = microformate.extract(html)Extract JSON-LD using JsonLdExtractor
masterUse
extruct.jsonld.JsonLdExtractorto extract JSON-LD structured data from HTML. Theextract(html)method returns a list of expanded JSON-LD nodes containing keys like@context,@type, and other schema properties.from extruct.jsonld import JsonLdExtractor html = '...<script type="application/ld+json">{"@type": "Person", "name": "John Doe"}</script>...' # truncated jslde = JsonLdExtractor() data = jslde.extract(html) # data returns a list of JSON-LD dictionariesUniformize metadata output structure
masterBy default, different syntaxes return data in their native formats. You can use the
uniform=Trueoption to transform the output ofmicroformat,opengraph,microdata,dublincore, andjson-ldinto a consistent structure:{ '@context': '...', '@type': '...', # All other properties as keys here }Note: The
rdfastructure is not currently uniformed.# Set uniform=True to get a standardized dictionary structure data = extruct.extract(r.text, base_url, syntaxes=['microdata', 'opengraph', 'rdfa'], uniform=True)Extract Microdata using MicrodataExtractor
masterUse
extruct.w3cmicrodata.MicrodataExtractorto extract W3C Microdata from HTML. Theextract(html)method returns a list of dictionaries, where each dictionary contains thetype(itemtype) and apropertiesdictionary mapping property names to their values.from extruct.w3cmicrodata import MicrodataExtractor html = '...'<html><body itemscope itemtype="http://n.whatwg.org/work"><img itemprop="work" src="images/house.jpeg"></body></html>...' # truncated for brevity mde = MicrodataExtractor() data = mde.extract(html) # data returns a list of items with 'type' and 'properties'Extract Open Graph metadata using OpenGraphExtractor
masterUse
extruct.opengraph.OpenGraphExtractorto extract Open Graph Protocol (OGP) metadata. Theextract(html)method returns a list containing a dictionary with anamespacemapping and apropertieslist. Thepropertieslist contains pairs of[property_name, value](e.g.,['og:title', 'Title Value']).from extruct.opengraph import OpenGraphExtractor html = '<meta property="og:title" content="Himanshu\'s Open Graph Protocol"/>...'<br> opengraphe = OpenGraphExtractor() data = opengraphe.extract(html)Extract RDFa using RDFaExtractor (experimental)
masterUse
extruct.rdfa.RDFaExtractorto extract RDFa from HTML. Note that this is marked as experimental and may requirehtml5libto be installed to function correctly. Theextract(html, base_url=...)method returns a list of expanded JSON-LD nodes. Providing abase_urlis useful for resolving relative resources.from extruct.rdfa import RDFaExtractor html = '<body prefix="dc: http://purl.org/dc/terms/">...</body>'<br> rdfae = RDFaExtractor() data = rdfae.extract(html, base_url='http://www.example.com/index.html')