2017-09-22 12 views
1

BeautifulSoupを使用して、1つのhtml表の最初と2番目の太字見出しの後の次の行の列1と列3のテキストを抽出しようとしています。太字のテキストはクラスまたはIDを持たず、その上下の行と同じレベルにあります。私はnext_siblingを使うべきだと思いますが、それをどうやってやっていくのかが分かりません。HTML表の各太字見出しの後に行の特定の列を抽出するBeautifulSoup

あなたはここで、テーブルのHTMLを見つけることができます:https://github.com/Tokaalmighty/topmover_table_html/blob/master/html

は、ここに私のロジックです:

soup=bs(f1,'html.parser') 
topmovers=soup.find('table',{'class':'topmovers'}) 

bold=topmovers.find_all('b') 
gainer=bold[0] 
gainer_name=bold.find('tr').next_sibling 
gcol1=gainer_name[0] 
gcol3=gainer_name[2] 

loser=bold[1] 
loser_name=bold.find('tr').next_sibling 
lcol1=loser_name[0] 
lcol3=loser_name[2] 

print(gcol1,gcol3,lcol1,lcol3) 
+0

あなたはhtml構造を共有できますか? – eLRuLL

答えて

1

あなたはstripped_strings

でテキストを取得、その後、次の 'TR' を選択し find_nextを使用することができます
soup=bs(f1,'html.parser') 
topmovers=soup.find('table',{'class':'topmovers'}) 

bold=topmovers.find_all('b') 
gainer=bold[0] 
gainer_name=gainer.find_next('tr') 
gainer_strings = list(gainer_name.stripped_strings) 
gcol1=gainer_strings[0] 
gcol3=gainer_strings[2] 

loser=bold[1] 
loser_name=loser.find_next('tr') 
loser_strings = list(loser_name.stripped_strings) 
lcol1=loser_strings[0] 
lcol3=loser_strings[2] 

print(gcol1, gcol3, lcol1, lcol3) 

McDermott International 6.55 Bill Barrett Corporation 2.87

関連する問題