从网站提取图像(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 website using Python
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.net as ahnet
 6
 7# Prepare output directory
 8output_dir = "output/"
 9os.makedirs(output_dir, exist_ok=True)
10
11# Open HTML document from URL
12with ah.HTMLDocument("https://docs.aspose.com/svg/net/drawing-basics/svg-color/") as doc:
13    # Collect all <img> elements
14    images = doc.get_elements_by_tag_name("img")
15
16    # Get distinct relative image URLs
17    urls = set(img.get_attribute("src") for img in images)
18
19    # Create absolute image URLs
20    abs_urls = [ah.Url(url, doc.base_uri) for url in urls]
21
22    for url in abs_urls:
23        # Create a network request
24        request = ahnet.RequestMessage(url)
25
26        # Send request
27        response = doc.context.network.send(request)
28
29        # Check if successful
30        if response.is_success:
31            # Extract file name
32            file_name = os.path.basename(url.pathname)
33
34            # Save image locally
35            with open(os.path.join(output_dir, file_name), "wb") as f:
36                f.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 website using Python
 2
 3import os
 4import aspose.html as ah
 5import aspose.html.net as ahnet
 6
 7# Define output directory
 8output_dir = "output/icons/"
 9os.makedirs(output_dir, exist_ok=True)
10
11# Open a document you want to extract icons from
12document = ah.HTMLDocument("https://docs.aspose.com/html/python-net/")
13
14# Collect all <link> elements
15links = document.get_elements_by_tag_name("link")
16
17# Leave only "icon" elements
18icons = [link for link in links if link.get_attribute("rel") == "icon"]
19
20# Create a distinct collection of relative icon URLs
21urls = {icon.get_attribute("href") for icon in icons}
22
23# Create absolute icon URLs
24abs_urls = [ah.Url(url, document.base_uri) for url in urls]
25
26for url in abs_urls:
27    # Create a request message
28    request = ahnet.RequestMessage(url)
29
30    # Extract icon
31    response = document.context.network.send(request)
32
33    # Check whether the response is successful
34    if response.is_success:
35        # Save icon to a local file system
36        file_path = os.path.join(output_dir, os.path.basename(url.pathname))
37        with open(file_path, 'wb') as file:
38            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”

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.