Zig
Zig
Extract all HTML Objects from a Web Page
See more MHT / HTML Email Examples
Demonstrates how to download a Web page (at a URL) and extract all HTML objects. Eg. images, links, CSS files, JavaScript files, etc.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
var success: bool = false;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const mht = try chilkat.Mht.init();
defer mht.deinit();
// Download a URL into an in-memory MHT web archive contained
// in a string variable.
// The following URL is randomly picked and was valid at the time of writing this example:
const mht_doc = mht.getMHT(alloc, "https://www.tetonlodge.com/") catch {
std.debug.print("{s}\n", .{try mht.getLastErrorText(alloc)});
return;
};
// Extract the HTML and embedded objects:
const unpack_dir = "C:/AAWorkarea/mhtTesting/";
const html_filename = "lodge.html";
const parts_subdir = "objects";
// Extract to C:/AAWorkarea/mhtTesting/lodge.html.
// images and other embedded objects are placed in
// C:/AAWorkarea/mhtTesting/objects. Directories are automatically
// created if they don't already exist.
success = if (mht.unpackMHTString(mht_doc, unpack_dir, html_filename, parts_subdir)) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try mht.getLastErrorText(alloc)});
} else {
std.debug.print("Unpacked!\n", .{});
}
}