How can I save BeautifulSoup object to disk as an XML file?

Viewed 290

I ran the following code:

url_req = "https://en.wikipedia.org/wiki/A._P._J._Abdul_Kalam"
response = requests.get(url=url_req,verify=True)
soup = BeautifulSoup(response.text, "lxml")

How can I save this soup object as an XML file to disk?

1 Answers

You can write the output of .prettify() to a file:

with open('file.xml', 'w') as f:
    f.write(soup.prettify())
Related