An HTML or MHTML document can reference images stored elsewhere. When GroupDocs.Parser extracts the images of such a document, it asks an external resource handler, if you give it one, before it loads each resource. The handler can let the resource load or skip it.
The handler receives an ExternalResourceLoadingArgs object:
Member
Meaning
args.uri
The resource as the document names it, for example installation.png or https://cdn.example/logo.png
args.skipped
Set it to True to skip the resource
The handler can be a function on_loading(args), an object with an on_loading(args) method, or a subclass of ExternalResourceHandler. Pass it to ParserSettings as external_resource_handler=, and the settings to the Parser constructor.
Warning
Loading of external resources is enabled by default, so parsing a document from an untrusted source may make outbound HTTP requests to hosts named inside that document.
The handler is consulted only when images are extracted from HTML and MHTML documents; other extraction methods and other formats do not route through it. To guarantee that no outbound request is made, block egress at the network or process level. See Network access and data privacy for the measured per-format behaviour of the engine.
Skip resources with a function
Here are the steps to handle loading of external resources:
Define a function that takes args and sets args.skipped for the resources to skip;
Create ParserSettings with external_resource_handler= your function;
Create the Parser object with the settings and call get_images.
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportParserSettingsdefon_loading(args):# args.uri names the resource; skip every image but oneifnotargs.uri.endswith("installation.png"):args.skipped=Truesettings=ParserSettings(external_resource_handler=on_loading)withParser("./document.html",settings)asparser:images=parser.get_images()ifimagesisNone:print("Image extraction isn't supported for this format")else:print("Images:",len(images))forimageinimages:print(image.file_type.extension)
The following sample file is used in this example: document.html
Expected behavior: The sample references three images; with the handler only one is extracted:
Images: 1
.png
Audit what a document asks for
A handler that skips nothing is a simple way to see which resources a document requests. The engine may ask about the same resource more than once, so collect the URIs in a set:
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportParserSettingsrequested=[]defon_loading(args):# Record every resource the document asks for, and load itrequested.append(args.uri)withParser("./document.html",ParserSettings(external_resource_handler=on_loading))asparser:print("Images:",len(parser.get_images()or[]))foruriinsorted(set(requested)):print("Requested:",uri)
The following sample file is used in this example: document.html
The .NET API defines the handler as a class with an OnLoading method. Subclassing ExternalResourceHandler and overriding on_loading works the same way, and keeps a reusable policy in one place:
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportExternalResourceHandler,ParserSettingsclassRemoteResourceBlocker(ExternalResourceHandler):"""Loads local resources and skips everything on the network."""defon_loading(self,args):ifargs.uri.startswith(("http://","https://","ftp://")):args.skipped=Truesettings=ParserSettings(external_resource_handler=RemoteResourceBlocker())withParser("./document.html",settings)asparser:print("Images:",len(parser.get_images()or[]))
The following sample file is used in this example: document.html