2017-12-10 9 views
-1
from urllib.request import urlopen 
from bs4 import BeautifulSoup 
html = urlopen("http://companyinfo.stock.naver.com/v1/company/c1010001.aspx?cmp_cd=056190") 
bsj = BeautifulSoup(html, 'html.parser') 
table = bsj.find_all("table" , {"class" : "gHead01 all-width"}). 

しかし、テーブルのcaption属性はブラインドです。soup.find( "table"、{"caption": "blind"})

The table.png

私はテーブルを取得するにはどうすればよいですか..

答えて

0

ない正確に。 <caption>要素は、テーブルの最初の子です。

指定した条件に一致するタグを返すためにBeautifulSoup.findに関数を渡すことができます。

soup = BeautifulSoup(markup, "html5lib") 

def table_having_caption_text(text): 
    def query(tag): 
     first_child = tag.next 
     return (
      tag.name=="table" 
      and first_child.name == 'caption' 
      and first_child.text == text 
     ) 
    return query 

print(
    soup.find(
     table_having_caption_text("Blind") 
    ) 
)