Beautiful Soup Conditional Query
<p>I am new to Beautiful Soup.
I need to get data from HTML file.</p>

<pre><code>&lt;div class="ques_ans_block"&gt;

   &lt;div class="question"&gt;
      &lt;p&gt;is this correct ?&lt;/p&gt;
   &lt;div&gt;
    &lt;p class="answer"&gt;&lt;/p&gt;

   &lt;div class="moreinfo" style="display: block;"&gt;
        &lt;p class="answer"&gt; &lt;p&gt;
        &lt;p class="answer"&gt;&lt;/p&gt;
   &lt;/div&gt;

&lt;/div&gt;
</code></pre>

<p>condition is , there can be "moreinfo" div  present or abscent.</p>

<p>so i need to find question and answer(including answer from "moreinfo" if present) innertext for each ques_ans_block ? </p>

<p>This will give output as json containing Question, answer and FaqID.</p>

<pre><code>import bs4
import json
import codecs
arrayList = []
bsp = bs4.BeautifulSoup(open('input.html'))
ques_ans_block = bsp.find_all("div", {"class": "ques_ans_block"})
s = ""
count = 1
for i in ques_ans_block:
    data = {}
    q = i.select('.question')   
    for a in q:
        s+=a.text+"\n"
    for a in q:
        a.extract()
    data["Question"] = s
    del i['.question']
    v = ""
    a = i.select('p')
    for a in a:
        v+=a.text+"\n"  
    a = i.select('li')
    for a in a:
        v+=a.text+"\n"
    data["Answer"] = v
    data["FaqId"] = count           
    print "\n"
    arrayList.append(data)
    count = count + 1
    s = ""

#print  arrayList
with codecs.open('output.json','wt','utf-8') as outfile:
    json.dump(arrayList, outfile, indent=4)
</code></pre>

python python-2.7 beautifulsoup