2017-11-15 12 views
0
soup = BeautifulSoup(html, "html.parser") # BeautifulSoup(markup, "lxml") 
items = soup.find_all("div","_3u1 _gli _uvb", recursive=True) 
    for item in items: 
     abouts = item.find_all("div", {"class":"_glo"}, recursive = True)[0].text 
    print (abouts) 

HTMLページ:PythonのV3、Beautifoulsoup - 複数のdivタグと同じ名前を持つ

  <div class="_glo"> 
      <div> 
       <div class="_ajw"> 
       <div class="_52eh"> 
        "text 
       </div> 
       </div> 
       <div class="_ajw"> 
       <div class="_52eh"> 
        "text" 
       </div> 
       </div> 
       <div class="_ajw"> 
       <div class="_52eh"> 
        "text" 
       </div> 
       </div> 
      </div> 
      </div> 

午後、私はbeautifullsoup、パイソンを使用してWebページをこすりしようとしています。別の変数に "text"文字列が必要です。私がaboutsを印刷するとき、私はそれを得る: "テキストテキストテキスト"私はそれを分離したい。これはあなたにこのような何か与える

items = soup.find_all('div', attrs={'class':'_ajw'}) 
dict = {} 
for i in range(len(items)): 
    dict['text'+str(i+1)] = item[i].find('div', attrs={'class':'_52eh'}).text 
print(dict) 

種類はこれを試してみてください

+0

を私はそれをしたい、最終的な予想される結果 – RomanPerekhrest

+0

を投稿:テキスト1: "テキスト" - > text2: "次のdivのテキスト" - > text3: "次のdivのテキスト" divクラスがなくなるまで...時々同じ名前の1 divタグ、他の時間は4,3、... –

+0

whileあなたが持っている すべてのdivの同じ 'text'値 - 最終出力は代表ではありません。より識別可能なコンテンツを投稿してください。 – RomanPerekhrest

答えて

0

について

{'text1': text, 'text2': text, 'text3': text} 
関連する問題