You use lxml parser, which is HTML parser. To parse XML you should use xml parser:
from bs4 import BeautifulSoup soup = BeautifulSoup('<svg></svg>', 'xml') print(soup) # ^^^^^^
From BeautifulSoup documentation:
Beautiful Soup presents the same interface to a number of different parsers, but each parser is different. Different parsers will create different parse trees from the same document. The biggest differences are between the HTML parsers and the XML parsers. Here’s a short document, parsed as HTML:
BeautifulSoup("<a><b /></a>") # <html><head></head><body><a><b></b></a></body></html>
Since an empty tag is not valid HTML, the parser turns it into a tag pair.
Here’s the same document parsed as XML (running this requires that you have lxml installed). Note that the empty tag is left alone, and that the document is given an XML declaration instead of being put into an tag.:
BeautifulSoup("<a><b /></a>", "xml") # <?xml version="1.0" encoding="utf-8"?> # <a><b/></a>
Source: Differences between parsers, emphasis mine.