Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/40bc666a5328d96d2eb25aae83f44a30 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/40bc666a5328d96d2eb25aae83f44a30 to your computer and use it in GitHub Desktop.
Extract data from a web page or any HTML document with Aspose.HTML for .Python via .NET

Web Scraping & Data Extraction Code Snippets

This repository contains ready-to-use Python code snippets referenced in the Aspose.HTML for Python via .NET documentation, specifically within the Data Extraction chapter. These gists showcase various approaches and techniques for effectively parsing, navigating, and extracting information from HTML documents using the Aspose.HTML for Python via .NET library.

Key Topics

  • Download images and SVGs from website – Programmatically extract various types of images from a website using Python.
  • Save File from URL – Extract and save a file from a URL programmatically by using the network capabilities of the Aspose.HTML for Python via .NET library.
  • Extract data from tables – Retrieve structured information from HTML tables.
  • Navigate and inspect HTML – Inspect HTML documents in detail and navigate elements efficiently using CSS selectors or XPath.

About Aspose.HTML for Python via .NET

Aspose.HTML for Python via .NET is a high-performance library that enables developers to create, edit, and convert HTML, SVG, EPUB, MHTML, and Markdown documents. It offers full control over DOM, CSS, and resources, making it ideal for cross-platform applications in Windows, Linux, and macOS. Using this API, developers can integrate advanced document conversion, rendering, and data extraction features into their Python applications without requiring external software.

Prerequisites

  • Python 3.5 or newer
  • .NET Core / .NET 5+ runtime installed
  • Supported OS: Windows, macOS, Linux
  • Aspose.HTML for Python via .NET installed from PyPI

How to Use These Examples

  1. Install the Aspose.HTML Python package:
   pip install aspose-html-net
  1. Clone or download this gist to your local machine.
  2. Set correct input/output paths, data directories, font folders, etc.
  3. Run your project to see the example in action.

Related Resources

To explore Aspose.HTML for Python via .NET features in more depth, download the library or get support using the official links below:

Aspose.HTML for Python via .NET – Data Extraction in Python
# Download external SVG images from HTML using Python
# Learn more: https://docs.aspose.com/html/python-net/extract-svg-from-website/
import os
import aspose.html as ah
import aspose.html.net as ahnet
# Define the output directory
output_dir = "output/svg/"
os.makedirs(output_dir, exist_ok=True)
# Open the HTML document from a URL
document = ah.HTMLDocument("https://products.aspose.com/html/python-net/")
# Collect all <img> elements
images = document.get_elements_by_tag_name("img")
# Create a distinct collection of relative image URLs
urls = set(img.get_attribute("src") for img in images)
# Filter only SVG images
svg_urls = [url for url in urls if url.endswith(".svg")]
# Convert relative URLs to absolute using Url from aspose.html
abs_urls = [ah.Url(url, document.base_uri) for url in svg_urls]
for url in abs_urls:
# Create a network request for the SVG
request = ahnet.RequestMessage(url.href)
# Send request to fetch the SVG
response = document.context.network.send(request)
# Check if request succeeded
if response.is_success:
# Determine local file path
file_path = os.path.join(output_dir, os.path.basename(url.pathname))
# Save SVG to local filesystem
with open(file_path, "wb") as f:
f.write(response.content.read_as_byte_array())
# Extract icons from website using Python
# Learn more: https://docs.aspose.com/html/python-net/extract-images-from-website/
import os
import aspose.html as ah
import aspose.html.net as ahnet
# Define output directory
output_dir = "output/icons/"
os.makedirs(output_dir, exist_ok=True)
# Open a document you want to extract icons from
document = ah.HTMLDocument("https://docs.aspose.com/html/python-net/")
# Collect all <link> elements
links = document.get_elements_by_tag_name("link")
# Leave only "icon" elements
icons = [link for link in links if link.get_attribute("rel") == "icon"]
# Create a distinct collection of relative icon URLs
urls = {icon.get_attribute("href") for icon in icons}
# Create absolute icon URLs
abs_urls = [ah.Url(url, document.base_uri) for url in urls]
for url in abs_urls:
# Create a request message
request = ahnet.RequestMessage(url)
# Extract icon
response = document.context.network.send(request)
# Check whether the response is successful
if response.is_success:
# Save icon to a local file system
file_path = os.path.join(output_dir, os.path.basename(url.pathname))
with open(file_path, 'wb') as file:
file.write(response.content.read_as_byte_array())
# Extract images from website using Python
# Learn more: https://docs.aspose.com/html/python-net/extract-images-from-website/
import os
import aspose.html as ah
import aspose.html.net as ahnet
# Prepare output directory
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Open HTML document from URL
with ah.HTMLDocument("https://docs.aspose.com/svg/net/drawing-basics/svg-color/") as doc:
# Collect all <img> elements
images = doc.get_elements_by_tag_name("img")
# Get distinct relative image URLs
urls = set(img.get_attribute("src") for img in images)
# Create absolute image URLs
abs_urls = [ah.Url(url, doc.base_uri) for url in urls]
for url in abs_urls:
# Create a network request
request = ahnet.RequestMessage(url)
# Send request
response = doc.context.network.send(request)
# Check if successful
if response.is_success:
# Extract file name
file_name = os.path.basename(url.pathname)
# Save image locally
with open(os.path.join(output_dir, file_name), "wb") as f:
f.write(response.content.read_as_byte_array())
# Extract inline SVGs from website using Python
# Learn more: https://docs.aspose.com/html/python-net/extract-svg-from-website/
import os
import aspose.html as ah
# Prepare the output directory
output_dir = "output/svg/" # Change this to your actual output directory
os.makedirs(output_dir, exist_ok=True)
# Open a document you want to extract inline SVG images from
with ah.HTMLDocument("https://docs.aspose.com/svg/net/drawing-basics/svg-shapes/") as document:
# Collect all inline SVG images
images = document.get_elements_by_tag_name("svg")
for i, image in enumerate(images):
# Save every SVG image to a local file system
with open(os.path.join(output_dir, f"{i}.svg"), 'w', encoding='utf-8') as file:
file.write(image.outer_html)
# Extract tables from website using Python
# Learn more: https://docs.aspose.com/html/python-net/data-extraction/
import os
import aspose.html as ah
# Define output directory
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Open HTML document from URL
with ah.HTMLDocument("https://docs.aspose.com/html/net/edit-html-document/") as doc:
# Get all <table> elements
tables = doc.get_elements_by_tag_name("table")
if tables.length > 0:
for i, table in enumerate(tables):
# Construct output file path
file_name = f"table{i}.htm"
file_path = os.path.join(output_dir, file_name)
# Create a new HTML document from the table's outer HTML
new_doc = ah.HTMLDocument(table.outer_html, file_path)
# Save the new document
new_doc.save(file_path)
else:
# Handle case where no tables are found
print("No tables found in the document.")
# Navigate and inspect HTML document using Python
# Learn more: https://docs.aspose.com/html/python-net/html-navigation/
import os
import aspose.html as ah
# Load a document from a file
data_dir = "data" # Change this to your actual data directory
document_path = os.path.join(data_dir, "html_file.html")
with ah.HTMLDocument(document_path) as document:
# Get the html element of the document
element = document.document_element
print(element.tag_name) # HTML
# Get the last element of the html element
element = element.last_element_child
print(element.tag_name) # BODY
# Get the first element of the body element
element = element.first_element_child
print(element.tag_name) # H1
print(element.text_content) # Header 1
# Navigate the HTML DOM using Python
# Learn more: https://docs.aspose.com/html/python-net/html-navigation/
import aspose.html as ah
# Prepare HTML code
html_code = "<span>Hello</span> <span>World!</span>"
# Initialize a document from the prepared code
with ah.HTMLDocument(html_code, ".") as document:
# Get the reference to the first child (first SPAN) of the BODY
element = document.body.first_child
print(element.text_content) # output: Hello,
# Get the reference to the whitespace between html elements
element = element.next_sibling
print(element.text_content) # output: " "
# Get the reference to the second SPAN element
element = element.next_sibling
print(element.text_content) # output: World!
# Download file from URL using Python
# Learn more: https://docs.aspose.com/html/python-net/save-file-from-url/
import os
import aspose.html as ah
import aspose.html.net as ahnet
# Define output directory
output_dir = "output/"
os.makedirs(output_dir, exist_ok=True)
# Create a blank document
doc = ah.HTMLDocument()
# Create a URL with the path to the resource you want to save
url = ah.Url("https://docs.aspose.com/html/images/handlers/message-handlers.png")
# Create a file request message
request = ahnet.RequestMessage(url)
# Extract file from URL
response = doc.context.network.send(request)
# Check whether the response is successful
if response.is_success:
# Save the file to a local file system
file_path = os.path.join(output_dir, os.path.basename(url.pathname))
with open(file_path, "wb") as file:
file.write(response.content.read_as_byte_array())
# Extract nodes Using CSS selector using Python
# Learn more: https://docs.aspose.com/html/python-net/html-navigation/
import aspose.html as ah
# Prepare HTML code
code = """
<div class='happy'>
<div>
<span>Hello,</span>
</div>
</div>
<p class='happy'>
<span>World!</span>
<p>I use CSS Selector.</p>
</p>
"""
# Initialize a document based on the prepared code
with ah.HTMLDocument(code, ".") as document:
# Create a CSS Selector that extracts all elements whose "class" attribute equals "happy" and their child <span> elements
elements = document.query_selector_all(".happy span")
# Iterate over the resulted list of elements
for element in elements:
print(element.text_content)
# output: Hello,
# output: World!
# How to use XPath to select nodes using Python
# Learn more: https://docs.aspose.com/html/python-net/html-navigation/
import aspose.html as ah
import aspose.html.dom.xpath as hxpath
# Prepare HTML code
code = """
<div class='happy'>
<div>
<span>Hello,</span>
</div>
</div>
<p class='happy'>
<span>World!</span>
</p>
"""
# Initialize a document based on the prepared code
with ah.HTMLDocument(code, ".") as document:
# Here we evaluate the XPath expression where we select all child SPAN elements from elements whose 'class' attribute equals to 'happy'
result = document.evaluate("//*[@class='happy']//span",
document,
None,
hxpath.XPathResultType.ANY,
None)
# Iterate over the resulted nodes
node = result.iterate_next()
while node is not None:
print(node.text_content)
node = result.iterate_next()
# output: Hello,
# output: World!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment