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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With