To parse XML in Python, use the standard library: import xml.etree.ElementTree as ET, root = ET.fromstring(data) for a string or bytes (or ET.parse("file.xml").getroot() for a file), then root.findall(), .find(), .text and .get(). When the document declares a namespace, pass it in a dictionary or nothing will match.
We tested five ways on 20 November 2026 with Python 3.12.10, on a real XML document: our own sitemap, quanticdata.io/sitemap.xml, 313,387 bytes and 2,618 URLs, downloaded through a QuanticData Residential Premium proxy. Then an RSS feed, the other XML you meet most when scraping.
Step by step with ElementTree
import requests, xml.etree.ElementTree as ET
PROXY = "http://USER-country-gb-sid-x01-ttl-30m:PASS@residentialpremium.quanticdata.io:8080"
r = requests.get("https://quanticdata.io/sitemap.xml",
proxies={"http": PROXY, "https": PROXY}, timeout=30)
root = ET.fromstring(r.content)
print(root.tag)
# {http://www.sitemaps.org/schemas/sitemap/0.9}urlset
NS = {"sm": "http://www.sitemaps.org/schemas/sitemap/0.9"}
print(len(root.findall("url"))) # 0
locs = [u.findtext("sm:loc", namespaces=NS) for u in root.findall("sm:url", NS)]
print(len(locs), locs[0]) # 2618 https://quanticdata.io/
- Parse bytes. Pass
r.content: the XML declaration names its own encoding. - Look at the root tag. The part in braces is the namespace. Here every element lives in it.
- Map a prefix to it and use the prefix in every path:
sm:url,sm:loc. - Read values with
findtext(), which returns None instead of failing when a child is missing: 2,512 of the 2,618 URLs had a lastmod.
The namespace trap
root.findall("url") returned 0 on a file with 2,618 url elements. No error, just nothing. Sitemaps, RSS with extensions, Atom, SOAP and office files all declare namespaces. Three ways to handle it in ElementTree, each of which found all 2,618 in our run:
- A prefix map, as above:
findall("sm:url", NS). - The full name in braces:
findall("{http://www.sitemaps.org/schemas/sitemap/0.9}url"). - A wildcard:
findall("{*}url")matches the name in any namespace.
Five parsers on the same file
| Library | Code | Result | Median parse + extract |
|---|---|---|---|
| lxml | etree.fromstring(b).xpath("//sm:loc/text()", namespaces=NS) | 2,618 | 3.8 ms |
| ElementTree (standard library) | ET.fromstring(b).findall("sm:url", NS) | 2,618 | 6.9 ms |
| xmltodict | xmltodict.parse(b)["urlset"]["url"] | 2,618 dicts | 18.8 ms |
| Beautiful Soup | BeautifulSoup(b, "xml").find_all("loc") | 2,618 | 102.6 ms |
| pandas | pd.read_xml(io.BytesIO(b)) | DataFrame (2618, 6) |
Medians of 10 runs each on this PC. lxml was fastest and adds XPath with conditions, for example //sm:loc[contains(., '/blog/')]/text(), which returned the 297 blog URLs; more in lxml XPath in Python. Beautiful Soup ignores namespaces by name (find_all("loc") just works) and needs lxml installed for its "xml" parser. pandas returned one row per url with the columns loc, lastmod, changefreq, priority, link and video, ready for analysis, with lastmod as text (dtype str in our run) until you convert it with pd.to_datetime, which turned it into datetime64; its HTML twin is pandas read_html.
XML to a dictionary
The autocomplete asks for "parse xml python to dict" more than anything else. xmltodict (pip install xmltodict) does it in one call:
import xmltodict
d = xmltodict.parse(r.content)
print(type(d["urlset"]["url"]).__name__, len(d["urlset"]["url"])) # list 2618
print(d["urlset"]["url"][0])
# {'loc': 'https://quanticdata.io/', 'lastmod': '2026-10-19'}
One catch: an element that appears once becomes a dictionary, and the same element twice becomes a list. Code that reads ["url"] must handle both, or pass force_list=("url",): on a one-url test file the value was a dict without it and a list with it. From the dictionary, json.dumps(d) gives XML to JSON; see parsing JSON in Python.
Parse an RSS feed
RSS 2.0 usually has no namespace on its main elements, so plain paths work:
f = requests.get("https://quanticdata.io/blog/feed.xml", proxies={"http": PROXY, "https": PROXY}, timeout=30)
channel = ET.fromstring(f.content).find("channel")
items = channel.findall("item")
print(len(items), items[0].findtext("title"), items[0].findtext("pubDate"))
# 153 Residential Proxy Success Rate on 20 Websites Wed, 18 Nov 2026 18:42:45 GMT
Attributes, missing values and pretty printing
Values often sit in attributes rather than text. A small catalogue shows the calls:
s = '<catalog><book id="b1" lang="en"><title>A Light in the Attic</title>' \
'<price currency="GBP">51.77</price></book></catalog>'
root = ET.fromstring(s)
book = root.find("book")
print(book.get("id"), book.attrib) # b1 {'id': 'b1', 'lang': 'en'}
print(root.find("book/price").get("currency"), float(root.findtext("book/price"))) # GBP 51.77
print(book.get("isbn", "n/a")) # n/a
new = ET.SubElement(root, "book", id="b2")
ET.SubElement(new, "title").text = "Tipping the Velvet"
ET.indent(root)
print(ET.tostring(root, encoding="unicode"))
.get() reads one attribute and takes a default for missing ones, .attrib gives them all as a dictionary, and text always comes back as a string, so convert numbers yourself. ET.indent() pretty-prints the tree before tostring(): our output came back indented two spaces per level, with the new book appended at the end.
Large files: iterparse
fromstring holds the whole tree in memory. For files of hundreds of megabytes, stream them and clear each element after use:
n = 0
for event, el in ET.iterparse("sitemap.xml", events=("end",)):
if el.tag == "{http://www.sitemaps.org/schemas/sitemap/0.9}url":
n += 1
el.clear()
print(n) # 2618
XML from websites: where proxies come in
Sitemaps and feeds are how scrapers discover pages: read the sitemap, then fetch each URL (the crawl itself is in how to web crawl with Python). Keep the request rate modest when you fetch the URLs a sitemap lists. We fetched these files through a UK Residential Premium session; QuanticData residential proxies take the country and session in the username, and mobile proxies put requests on carrier IPs. Try them with the Residential Basic free trial.
Parsing XML from untrusted sources has its own risk: Python's documentation warns about malicious XML such as entity expansion bombs. For files you do not control, the same documentation points to the defusedxml package.


