Skip to content

Instantly share code, notes, and snippets.

@kitswas
Last active March 8, 2024 18:56
Show Gist options
  • Save kitswas/9b16c951dfe2d13ff2329ea2b6b2b573 to your computer and use it in GitHub Desktop.
Save kitswas/9b16c951dfe2d13ff2329ea2b6b2b573 to your computer and use it in GitHub Desktop.
This Python script extracts all the Qt classnames from the Qt6 documentation at https://doc.qt.io/qt-6/classes.html and writes them to a file called qt-classes.txt.
# This script extracts all the Qt classnames from the Qt documentation
# at https://doc.qt.io/qt-6/classes.html
# and writes them to a file called qt-classes.txt
import importlib
from os import system
# Modules required by this script and their pip names
modules = {"requests": "requests", "bs4": "beautifulsoup4"}
modules_installed = []
def setup():
"""
Check if required modules are available.
If not, install them.
"""
for module in modules.keys():
try:
importlib.import_module(module)
except ImportError as e:
print(e)
print("Module {} not found. Installing...".format(modules.get(module)))
system("pip install {} --quiet".format(modules.get(module)))
modules_installed.append(modules.get(module))
setup()
import requests
from bs4 import BeautifulSoup, NavigableString, PageElement, Tag
import json
# Get the HTML from the Qt documentation
url = "https://doc.qt.io/qt-6/classes.html"
response = requests.get(url)
html = response.content
# Parse the HTML
soup = BeautifulSoup(html, "html.parser")
# Find all the classnames
# The classnames are in <dd> tags
# As children of a <div> tag with id="details"
# Find the <div> tag with id="details"
result = soup.find("div", id="details")
if result is not None:
# print(result.prettify())
# Find all the <dd> tags
div_details: PageElement = result
dd_tags = div_details.find_all("dd")
# Store the classnames in a set
classnames = set()
for dd_tag in dd_tags:
# Remove the text in brackets
# e.g. QAbstractAnimation (Qt3DAnimation)
# becomes QAbstractAnimation
classname: str = dd_tag.text.split(" ")[0]
classnames.add(classname)
classnames = sorted(classnames) # Sort the classnames
# Print the list of classnames
print("Found {} classes".format(len(classnames)))
print(json.dumps(classnames))
# Write the list of classnames to a file
with open("qt-classes.txt", "w") as f:
for classname in classnames:
f.write(classname + "\n")
def cleanup():
"""
Cleanup installed modules.
"""
if len(modules_installed) > 0:
print("Uninstalling modules installed by this script...")
for module in modules_installed:
system("pip uninstall {} -y".format(module))
cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment