Python--Export Parsed XML to txt file using

时间:2016-10-20 19:38:22

标签: python xml beautifulsoup

I'm parsing text from an XML file. Parsing works well, and I can print the results in full, but when I try to write the text into a text document, all I get in the document is the last item.

 from bs4 import BeautifulSoup
 import urllib.request
 import sys

 req = urllib.request.urlopen('file:///C:/Users/John/Desktop/Dow%20Jones/compaq%20neg%201.xml')

 xml = BeautifulSoup(req, 'xml')

 for item in xml.findAll('paragraph'):
     sys.stdout = open('CN1.txt', 'w')
     print(item.text)
     sys.stdout.close()

What am I missing here?

1 个答案:

答案 0 :(得分:1)

It looks like you are opening the file every time you go through the loop, which I am surprised it let you do. When it opens the file, it is is opening it in write mode and therefore is wiping out everything that was in it on the last pass through the loop.

相关问题