从网站提取图像(Python)

在本文中,我们将探讨如何使用 Aspose.HTML for Python via .NET 从网站提取各种类型的图像。借助该 Python 库,您可以高效地下载网站图像,而无需手动搜索。让我们开始使用编程方式提取图像吧!

从网站提取图像

大多数 HTML 文档中的图片都是通过 <img> 元素表示的。下面的示例展示了如何使用 Aspose.HTML for Python via .NET 查找该元素并下载图像。

  1. 使用 HTMLDocument(Url) 构造函数初始化 HTMLDocument 对象,并提供要提取图像的网页 URL。
  2. 调用 get_elements_by_tag_name(“img”) 方法获取文档中所有 <img> 元素。此方法返回文档中找到的所有 <img> 元素集合。
  3. 通过遍历收集到的 <img> 元素并使用 get_attribute(“src”) 方法提取唯一的图像 URL。将这些 URL 存入集合以避免重复。
  4. 使用 Url 类和文档的 base_uri 属性创建绝对图像 URL,确保它们能够正确请求。
  5. 对每个绝对图像 URL,创建一个 RequestMessage 对象并发送网络请求以获取图像。
 1# Extract images from a website in Python
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.net as ahnet
 6
 7# Prepare the output directory
 8output_dir = "output"
 9os.makedirs(output_dir, exist_ok=True)
10
11# Load the webpage and collect image URLs
12with ah.HTMLDocument("https://docs.aspose.com/html/net/tutorial/html-colors/") as document:
13    # Collect all <img> elements
14    images = document.get_elements_by_tag_name("img")
15
16    urls = {image.get_attribute("src") for image in images if image.get_attribute("src")}
17
18    # Download each image
19    for image_url in urls:
20        url = ah.Url(image_url, document.base_uri)
21        file_name = os.path.basename(url.pathname)
22
23        if not file_name:
24            continue
25
26        with ahnet.RequestMessage(url) as request:
27            with document.context.network.send(request) as response:
28                if response.is_success:
29                    with open(os.path.join(output_dir, file_name), "wb") as file:
30                        file.write(response.content.read_as_byte_array())

注意: 在商业用途前,请确保遵守版权法并获得适当授权。我们不支持在未获许可的情况下使用他人文件进行商业活动。

提取图标

图标是 HTML 文档中使用 <link> 元素并将 rel 属性设为 icon 的一种图像。下面展示如何使用 Aspose.HTML for Python via .NET 从网站提取图标:

  1. 使用 HTMLDocument(Url) 构造函数创建 HTMLDocument 实例,并传入要提取图标的网页 URL。
  2. 调用 get_elements_by_tag_name(“link”) 方法收集所有 <link> 元素。
  3. 使用 get_attribute(“rel”) 方法获取 rel 属性的值,过滤出 rel="icon" 的元素。
  4. 从每个图标链接元素中提取 href 属性,获取相对 URL。使用文档的 base URI 将相对 URL 转换为绝对 URL。
  5. 对每个绝对图像 URL,创建 RequestMessage 对象并发送请求以下载图标。
 1# Extract icons from a website in Python
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.net as ahnet
 6
 7# Prepare the output directory
 8output_dir = os.path.join("output", "icons")
 9os.makedirs(output_dir, exist_ok=True)
10
11# Load the webpage and collect icon URLs
12with ah.HTMLDocument("https://docs.aspose.com/html/python-net/") as document:
13    links = document.get_elements_by_tag_name("link")
14    icon_urls = {
15        link.get_attribute("href")
16        for link in links
17        if "icon" in (link.get_attribute("rel") or "").lower().split()
18        and link.get_attribute("href")
19    }
20
21    # Download each icon
22    for icon_url in icon_urls:
23        url = ah.Url(icon_url, document.base_uri)
24        file_name = os.path.basename(url.pathname)
25
26        if not file_name:
27            continue
28
29        with ahnet.RequestMessage(url) as request:
30            with document.context.network.send(request) as response:
31                if response.is_success:
32                    file_path = os.path.join(output_dir, file_name)
33                    with open(file_path, "wb") as file:
34                        file.write(response.content.read_as_byte_array())

您可以使用这些 Python 示例自动化从网站提取所有图像。此功能在归档、研究、分析网页内容或任何需要批量获取图像的场景中非常有价值。它同样适用于网页设计师和开发者,以便快速获取网站资源。

下载 Aspose.HTML for Python via .NET 库,即可快速、轻松地操作 HTML 文档。该 Python 库能够创建、修改、提取数据、转换以及渲染 HTML 文档,无需外部软件。它支持 EPUB、MHTML、XML、SVG、Markdown 等常用格式,并可渲染为 PDF、DOCX、XPS 以及图像文件。

您可以从 GitHub 下载完整示例和数据文件。

Aspose.HTML 提供一套 HTML Web Applications,其中包含免费转换器、合并器、SEO 工具、HTML 代码生成器、URL 工具等。该应用在任何带浏览器的操作系统上均可使用,无需额外安装软件。使用这些工具可简化工作流,提高生产力。

Text “HTML Web Applications”