网站转 HTML – 使用 C# 保存网站
网站转 HTML
虽然如今有线互联网或 Wi-Fi 随处可见,但有时您可能无法连接互联网,例如在地铁站之间、旅行途中或飞行途中。为了能够在没有网络连接的情况下访问信息,您需要保存不同网站的内容,以便离线使用,也许是为了阅读、研究或娱乐。
将网站转换为 HTML 有几个原因:
- 无需网络连接即可访问信息;
- 从网站中提取有用信息,用于分析或其他目的;
- 记录页面内容,以免数据因某种原因不可用。也许你有过书签被破坏的经历;
- 将静态 HTML 网站转移到另一台主机上;
- 将内容存档并创建备份;
- 学习目的,如设计或网页模板研究等。
本文提供了使用 Aspose.HTML for .NET API 保存网站或保存网页的方法。您可以自定义保存过程–保存整个网站或保存一个网页。
如何保存网页
您可以使用 Aspose.HTML for .NET 库 将网站转换为 HTML,以便离线阅读,而不会有任何麻烦。您需要采取以下几个步骤:
- 使用
HTMLDocument(
Url
) 构造函数从 URL 加载一个HTMLdocument
对象,将网页转换为 HTML。 - 创建
HTMLSaveOptions 类的实例,并设置所需的属性以自定义保存过程。如果不初始化
HTMLSaveOptions
,程序将使用默认保存选项,如下例所示。 - 调用
Save(
savePath
) 或 Save(savePath
,options
) 方法离线保存网站。
下面的 C# 示例展示了如何保存网页。在默认保存选项下,您只能保存一个包含相关资源的独立网页。请注意,只有与网站页面位于同一域的资源才会被保存。
1// Extract and save a wab page with default save options in C#
2
3// Initialize an HTML document from a URL
4using HTMLDocument document = new HTMLDocument("https://docs.aspose.com/html/net/message-handlers/");
5
6// Prepare a path to save the downloaded file
7string savePath = Path.Combine(OutputDir, "root/result.html");
8
9// Save the HTML document to the specified file
10document.Save(savePath);
使用本文介绍的功能,您既可以保存单个页面,也可以保存包含资源的整个网站。要自定义保存过程,可以为 HTMLSaveOptions 对象指定资源处理选项。
资源处理选项
使用 HTMLSaveOptions 和 ResourceHandlingOptions 类可以自定义保存过程。例如,您可以控制要处理的页面深度,保存整个网站或只保存一个网页。
下表总结了 ResourceHandlingOptions 类的主要属性。它们提供了在使用 API 加载 HTML 文档时处理最大页面深度、应用限制或处理外部资源(如图像、CSS 文件和 JavaScript)的选项。
Property | Description |
---|---|
Default | Gets or sets an enum representing the default resource handling method. Currently, Save , Ignore , and Embed values are supported. The default value is Save , meaning the resource will be saved as a file. |
JavaScript | Represents the way scripts are handled. Currently, Save , Ignore , Discard , and Embed values are supported. The default value is Save , meaning the resource will be saved as a file. |
MaxHandlingDepth | This property contains information about the maximum depth of pages that will be handled. Using this property, you can manipulate the depth of pages that will be handled and save entire website or only save a web page. A depth of 1 means only pages directly referenced from the saved document will be handled. Setting this property to -1 will lead to the handling of all pages. The default value is 0. |
PageUrlRestriction | Contains information about restrictions applied to URLs of handled pages. The default value is RootAndSubFolders , meaning only resources in the root and subfolders are processed. |
ResourceUrlRestriction | This property contains information about restrictions applied to URLs of handled resources such as CSS, js, images, etc. The default value is SameHost , meaning only resources in the same host are processed. |
使用 JavaScript
属性将网站转换为 HTML
Aspose.HTML 提供了控制脚本保存逻辑的功能。脚本可以保存在单独的文件中、嵌入或从生成的文档中删除。下面的 C# 示例展示了如何保存网站并将所有 JavaScript 保存到生成的 HTML 文档中。
要从 URL 中保存网站,应采取以下几个步骤:
- 使用
HTMLDocument(
Url
) 构造函数从 URL 加载 HTMLdocument 对象,将网站转换为 HTML。 - 创建一个
HTMLSaveOptions 类实例,并将
JavaScripts 属性设置为
ResourceHandling.Embed
值。 1.调用 Save(savePath
,options
) 方法离线保存网站。
在下面的示例中,ResourceHandling.Embed
(资源处理嵌入)选项指定在保存时将任何 JavaScript
资源嵌入 HTML 文档。这意味着生成的 HTML 文件将包含文档中的所有 JavaScript
资源,而不是将它们作为外部文件引用。
1// Download website using HTMLSaveOptions in C#
2
3// Initialize an HTML document from a URL
4using HTMLDocument document = new HTMLDocument("https://docs.aspose.com/html/net/message-handlers/");
5
6// Create an HTMLSaveOptions object and set the JavaScript property
7HTMLSaveOptions options = new HTMLSaveOptions
8{
9 ResourceHandlingOptions =
10 {
11 JavaScript = ResourceHandling.Embed
12 }
13};
14
15// Prepare a path to save the downloaded file
16string savePath = Path.Combine(OutputDir, "rootAndEmbedJs/result.html");
17
18// Save the HTML document to the specified file
19document.Save(savePath, options);
使用 MaxHandlingDepth
属性将网站转换为 HTML
MaxHandlingDepth 属性指定了要加载和处理的 HTML 文档元素层次结构的最大深度。API 不会加载或处理超过此深度的任何元素。因此,MaxHandlingDepth(最大处理深度)可优化保存过程的性能,通过限制要处理的元素数量,帮助减少 API 所需的内存和处理能力。
默认情况下,只有打开的文档及其资源会保存为单独的网页,但您可以使用 MaxHandlingDepth
属性控制处理深度。下面的示例显示了如何不仅保存文档,还保存其链接的所有页面,以及其 URL 相对于该页面 URL 的嵌套页面。当该属性设置为 1 时,即只保存 HTML 文档层次结构中深度为 1 的元素,让我们看看 C# 示例:
1// Save a website with limited resource depth using C#
2
3// Load an HTML document from a URL
4using HTMLDocument document = new HTMLDocument("https://docs.aspose.com/html/net/message-handlers/");
5
6// Create an HTMLSaveOptions object and set the MaxHandlingDepth property
7HTMLSaveOptions options = new HTMLSaveOptions
8{
9 ResourceHandlingOptions =
10 {
11 MaxHandlingDepth = 1
12 }
13};
14
15// Prepare the output path for saving the downloaded content
16string savePath = Path.Combine(OutputDir, "rootAndAdjacent/result.html");
17
18// Save the document along with adjacent resources only
19document.Save(savePath, options);
使用 PageUrlRestriction
属性保存网站
Aspose.HTML for .NET 提供了多种选项,用于过滤网站保存页面的 URL。当保存 HTML 文档时, PageUrlRestriction 属性可限制从特定 URL 或域加载网页。
默认情况下,PageUrlestriction
属性设置为 RootAndSubFolders
,即只处理根目录和子文件夹中的页面。不过,你也可以将此属性设置为其他值–SameHost
或None
。将其设置为 None
后,就可以从任何域加载网页,这些域的 URL 都在已保存的网站上。谨慎使用此属性至关重要,因为允许从任何域加载网页都会增加安全漏洞的风险。
在下面的示例中,除文档外,还将保存 HTML 文档引用的所有网页以及同一域中的网页:
1// Save a website with restricted resource URLs using C#
2
3// Initialize an HTML document from a URL
4using HTMLDocument document = new HTMLDocument("https://docs.aspose.com/html/net/message-handlers/");
5
6// Configure HTMLSaveOptions with restricted resource handling
7HTMLSaveOptions options = new HTMLSaveOptions
8{
9 ResourceHandlingOptions =
10 {
11 MaxHandlingDepth = 1,
12 PageUrlRestriction = UrlRestriction.SameHost
13 }
14};
15
16// Prepare the output path for the saved content
17string savePath = Path.Combine(OutputDir, "rootAndManyAdjacent/result.html");
18
19// Save the HTML document and allowed resources to the specified path
20document.Save(savePath, options);
您可以从 GitHub 下载完整的 C# 示例和数据文件。
Aspose.HTML 提供 HTML 网络应用程序,它是免费转换器、合并器、搜索引擎优化工具、HTML 代码生成器、URL 工具等的在线集合。这些应用程序可在任何装有网络浏览器的操作系统上运行,无需安装任何其他软件。无论你身在何处,都能轻松转换、合并、编码、生成 HTML 代码,从网络中提取数据,或从搜索引擎优化的角度分析网页。使用我们的 HTML 网络应用程序集来处理您的日常事务,让您的工作流程天衣无缝!