Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python xml iterate over questions and answer

I have survey responses stored in xml and unfortunately the xml is not uniformly built. See below xml.

I would like to iterate over divs and then pull all <b> elements out as the questions but I am not sure how to deal with the answers as they are sometimes included in a sub <div> and sometimes not.

I was thinking of using elementtree's intertext or beautiful soup. But BeautifulSoup returns all divs including the inner ones if I do a soup.find_all('div'). tree.itertext() kind of works, but I don't want to have too many nested loops if possible.

Any suggestions how to best handle this situation?

 <html>
 <body>
  <div>
   <b>Question 1: What is your name?</b>
   My name is Peter.
  </div>
  <div>
   <b>Question 2: What is your native language?</b>
   <div>Esperanto</div>
  </div>
 </body>
</html>
like image 251
Eric Avatar asked Jul 31 '26 03:07

Eric


1 Answers

Iterate over top-level divs, extract the question text from the b tag, extract the answer from the next sibling or from the text of the next sibling of the next sibling:

from bs4 import BeautifulSoup

soup = BeautifulSoup("""
<html>
 <body>
  <div>
   <b>Question 1: What is your name?</b>
   My name is Peter.
  </div>
  <div>
   <b>Question 2: What is your native language?</b>
   <div>Esperanto</div>
  </div>
 </body>
</html>
""")

for div in soup.find('body').findAll('div', recursive=False):
    question = div.find('b')
    print question.text
    print question.nextSibling.strip() or question.nextSibling.nextSibling.text.strip()

prints:

Question 1: What is your name?
My name is Peter.
Question 2: What is your native language?
Esperanto
like image 53
alecxe Avatar answered Aug 01 '26 20:08

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!