The documentation is relatively straightforward.
You want to send a GET request with their username:
import requests
username = "Bob"
resp = requests.get(f"https://api.mojang.com/users/profiles/minecraft/{username}")
uuid = resp.json()["id"]
print(f"Bob's current UUID is {uuid}")
Optionally, you can include a UNIX timestamp to get the username's UUID at a specific point in time:
username = "Alice"
# UNIX timestamp that equates to 01/01/2018
timestamp = 1514764800
resp = requests.get(f"https://api.mojang.com/users/profiles/minecraft/{username}?at={timestamp}")
uuid = resp.json()["id"]
print(f"Alice's UUID on 01/01/2018 was {uuid}")
Other Solution (third party library)
Alternatively, you can use my newly released Mojang package, if you don't want to deal with the HTTP requests, JSON, and other web junk in your code.
Install it with pip by running the following console command:
python -m pip install mojang
Usage:
from mojang import MojangAPI
uuid = MojangAPI.get_uuid("Alice")
print(f"Alice's UUID is {uuid}")
# or with a timestamp
uuid = MojangAPI.get_uuid("Alice", timestamp=1514764800)
print(f"Alice's UUID on 01/01/2018 was {uuid}")