Back to Python
2026-01-115 min read

HTML (Python Programming)

Learn HTML (Python Programming) step by step with clear examples and exercises.

Title: Python for Web Development - Creating and Manipulating HTML using Python

Why This Matters

Python is a versatile language used not just for scripting but also for web development. One of its key uses is generating dynamic HTML content, which can be particularly useful when you need to automate the creation of web pages or manipulate existing ones. In this lesson, we will explore how Python can be used to create and manipulate HTML content.

Python's ability to work with HTML allows developers to build applications that interact with websites, scrape data, and even create entire websites from scratch. This makes it an essential tool for anyone interested in web development or data analysis.

Prerequisites

Before diving into creating and manipulating HTML with Python, it's important that you have a good understanding of the following:

  1. Python programming basics (variables, data types, functions, loops, and control structures)
  2. Familiarity with HTML syntax and structure
  3. Basic knowledge of web development concepts such as URLs, requests, and responses
  4. Understanding of how to install and use libraries in Python

Core Concept

Python provides several libraries to work with HTML, but the most popular ones are BeautifulSoup and lxml. Both libraries allow you to parse HTML and XML documents, locate specific elements using various methods, and manipulate them. To install them, use the following commands:

pip install beautifulsoup4
pip install lxml

Let's take a simple example of parsing an HTML document:

from bs4 import BeautifulSoup
import requests

Make a request to the website

r = requests.get("http://example.com")

r_content = r.content

Create a BeautifulSoup object and specify the parser (lxml is recommended)

soup = BeautifulSoup(r_content, "lxml")

Locate specific elements using various methods

title = soup.title # Selects the title tag directly

headings = soup.find_all("h1", class_="my-class") # Find all h1 heading tags with a specific class

links = soup.find_all("a", href=True) # Find all anchor tags (links) that have an 'href' attribute


In the example above, we used BeautifulSoup to parse an HTML document and locate specific elements using CSS selectors. You can find more information about CSS selectors [here](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors).

Worked Example

Let's create a simple HTML page using Python:

from jinja2 import Environment, FileSystemLoader

Create an environment for the Jinja2 template engine

env = Environment(loader=FileSystemLoader("."))

Load the template file (template.html in the current directory)

template = env.get_template('template.html')

Render the template with data (title and content)

output = template.render(title="Welcome to My Website", content="This is a simple web page created using Python.")

Write the output to an HTML file

with open("index.html", "w") as f:

f.write(output)


In this example, we used Jinja2, another popular Python library for templating. The template file (template.html) might look like this:

{% block title %}{{ title }}{% endblock %}

{% block content %}{{ content }}{% endblock %}

Common Mistakes

  1. Forgetting to import necessary libraries: Make sure you have imported BeautifulSoup, lxml (if using it), requests (or any other library you're using) at the beginning of your script.
  2. Not specifying the parser correctly: If you encounter errors while parsing HTML, ensure that you have specified the correct parser (lxml, html.parser, or xml).
  3. Misunderstanding CSS selectors: BeautifulSoup uses CSS selectors to locate elements in an HTML document. Make sure you understand how they work and are using them correctly.
  4. Not handling exceptions: When working with requests, make sure to handle exceptions such as ConnectionError or TimeoutError to ensure your script can recover gracefully if it encounters issues.
  5. Ignoring whitespace: Be aware that BeautifulSoup normalizes whitespace when parsing HTML documents, so you may need to account for this in your code.
  6. Not properly escaping output: When rendering templates with user-provided data, make sure to use Jinja2's built-in escape filters (e.g., |safe) to prevent cross-site scripting (XSS) attacks.
  7. Not respecting robots.txt rules: Some websites may have rules in their robots.txt file that prohibit web scraping. Make sure to consult the website's robots.txt file and respect its rules to avoid being blocked.

Practice Questions

  1. Write a Python script that fetches the source code of Google's homepage and counts the number of h1 headings on the page using BeautifulSoup.
  2. Modify the template from the worked example to include a navigation bar with links to other pages on your website.
  3. Write a Python script that scrapes the titles and URLs of all articles from a news website (e.g., BBC News) using BeautifulSoup, and stores them in a CSV file.
  4. Write a Python script that takes a list of URLs as input and fetches their content using requests, then parses the HTML using BeautifulSoup to find specific elements (e.g., images, prices, or text).
  5. Create a simple web server using Flask that serves dynamic HTML pages generated by Jinja2 templates.

FAQ

  1. What is the difference between BeautifulSoup and lxml?
  • BeautifulSoup is a Python library for parsing HTML and XML documents, while lxml is an efficient parser library that can be used with BeautifulSoup. Lxml provides faster performance than other parsers like html.parser or xml.etree.ElementTree.
  1. How do I handle missing or broken links in my web scraping script?
  • You can use the is_valid_url function from the urllib.parse module to check if a URL is valid before making requests, and handle exceptions when encountering broken links.
  1. What are some best practices for writing clean and maintainable web scraping scripts?
  • Use meaningful variable names, comment your code, and modularize your script by breaking it into smaller functions. Also, be mindful of the website's robots.txt file and respect its rules to avoid being blocked. Additionally, consider using a library like Scrapy for larger scraping projects, as it provides tools for handling requests, parsing HTML, and managing concurrent tasks more efficiently.
HTML (Python Programming) | Python | XQA Learn