2014-12-20 180 views
0

我需要使用BeautifulSoup解析HTML文件。該HTML看起來像這樣:解析BeautifulSoup html標記

<div class="entry_container"> 

     <div class="entry lang_en-gb" id="turn-over_1"> 
      <span class="inline"> 
      <h1 class="hwd">turn over</h1> 
      </span> 
      <div class="hom" id="turn-over_1.1"> 
      <span class="gramGrp"><span class="pos">intransitive verb</span></span> 
      <div class="sense"><span class="bold">1 </span><span class="gramGrp"><span class="colloc"><span>[</span>person<span>]</span></span></span><span class="lbl"><span> (</span>in bed<span>)</span></span><span> </span><span class="cit lang_fr"><span class="quote">se retourner</span></span><span class="cit" id="turn-over_1.2"><span>; </span></span></div> 

      <div class="sense"><span> <br/></span><span class="bold">2 </span><span class="gramGrp"><span class="colloc"><span>[</span>car<span>]</span></span></span><span> </span><span class="cit lang_fr"><span class="quote">se retourner</span></span><span>, </span><span class="cit lang_fr"><span class="quote">faire un tonneau</span></span><span class="cit" id="turn-over_1.3"><span>; </span></span></div> 

      <div class="sense"><span> <br/></span><span class="bold">3 </span><span class="lbl"><span>(= </span>switch TV channels<span>)</span></span><span> </span><span class="cit lang_fr"><span class="quote">changer de chaîne</span></span><span class="cit" id="turn-over_1.4"><span>; </span></span></div> 

      </div> 

      <div class="hom" id="turn-over_1.5"> 
      <span> <br/>▶ </span><span class="gramGrp"><span class="pos">transitive verb</span></span> 
      <div class="sense"> 
       <span class="bold">1 </span> 
       <div class="sense"><span class="bold"> a </span><span class="gramGrp"><span class="colloc"><span>[</span><span>+ </span>object<span>]</span></span></span><span> </span><span class="cit lang_fr"><span class="quote">retourner</span></span><span class="cit" id="turn-over_1.6"><span>; </span></span></div> 

       <div class="sense"><span class="bold"> b </span><span class="gramGrp"><span class="colloc"><span>[</span><span>+ </span>page<span>]</span></span></span><span> </span><span class="cit lang_fr"><span class="quote">tourner</span></span></div> 

       <div class="sense"><span class="bold"> c </span><span class="gramGrp"><span class="colloc"><span>[</span><span>+ </span>tape<span>]</span></span></span><span> </span><span class="cit lang_fr"><span class="quote">changer de face</span></span><span class="cit" id="turn-over_1.7"><span>; </span></span></div> 

      </div> 

      <div class="sense"><span> <br/></span><span class="bold">2 </span><span class="lbl"><span>(= </span>hand over<span>)</span></span><span> </span><span class="cit lang_fr"><span class="quote">remettre</span></span><span class="cit" id="turn-over_1.8"><span>; </span></span><span class="cit" id="turn-over_1.9"><span>; </span></span></div> 

      </div>  
     </div> 

    </div> 

我需要檢索的解析每個div class="hom"

結果的POS(span class="pos")和意義(每個<div class="sense">)可能是這樣的:

enter image description here

現在,我已經嘗試此代碼:

for gramGrp in entryContentHTML.find_all('div',attrs={"class":u"hom"}): 
    for pos in gramGrp.find('span',attrs={"class":u"gramGrp"}).find('span',attrs={"class":u"pos"}): 
    print pos 

但輸出是:

intransitive verb 
intransitive verb 
transitive verb 

回答

1

你將不得不收拾輸出,但這會得到你所需要的:

from bs4 import BeautifulSoup 
soup = BeautifulSoup(html) 

res= (["\n".join(s.strip() for s in x.text.splitlines()).replace(";","") for x in  soup.find_all("div", {"class":"hom"})]) 
print("\n".join(res)) 


intransitive verb 
1 [person] (in bed) se retourner 
2 [car] se retourner, faire un tonneau 
3 (= switch TV channels) changer de chaîne 

▶ transitive verb 

1 
a [+ object] retourner 
b [+ page] tourner 
c [+ tape] changer de face 

2 (= hand over) remettre