如何使用Beautiful Soup提取标签的名称?

时间:2018-05-27 19:13:34

标签: python html beautifulsoup

鉴于以下html文档,如何提取字符串“CLN8”?

<tr data-table-chart-alt-symbol="CLN8" data-table-chart-symbol="@CL.1">
<td class="first text" data-field="symbol"><a href="//www.cnbc.com/quotes/?symbol=%40CL.1">OIL</a></td>
<td data-field="last"></td>
<td class="arrow" data-field="change_arrow"><div class="icon unch">---</div></td>
<td data-field="change"></td>
<td data-field="change_pct"></td>
<td data-field="volume"></td>
</tr>

1 个答案:

答案 0 :(得分:1)

您可以使用BeautifulSoup.find

from bs4 import BeautifulSoup as soup
d = soup(html, 'html.parser').find('tr')['data-table-chart-alt-symbol']

输出:

'CLN8'