Creating an XML Document with BeautifulSoup

In all the examples and tutorials I've seen in BeautifulSoup, an HTML / XML document is transferred and a soup object is returned, which can then be used to modify the document. However, how can I use BeautifulSoup to create an HTML / XML document from scratch? In other words, I have data that I would like to put in the XML file, but the XML file does not exist yet, and I would like to create it from scratch. How can i do this?

+5
source share
1 answer

Just create an empty object BeautifulSoup():

soup = BeautifulSoup()

and start adding items:

soup.append(soup.new_tag("a", href="http://www.example.com"))

For XML, you can start with the XML header using the tree constructor xml:

soup = BeautifulSoup(features='xml')

lxml. .is_xml BeautifulSoup ( ).

+6

All Articles