Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 392
palash Avatar asked Jan 22 '26 11:01

palash


1 Answers

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

with open('file.xml', 'w') as f:
    f.write(soup.prettify())
like image 173
MendelG Avatar answered Jan 24 '26 02:01

MendelG