美丽的汤类选择器

时间:2020-03-14 10:05:25

标签: python-3.x beautifulsoup

我在html页面中有两种类型的表。

<table class="ui-table hp-formTable ui-table_type1 ui-table_sortable">
<table class="ui-table hp-raceRecords ui-table_type2">

我只需要选择第一个。 如果我尝试这样的事情:

BSdata1 = BeautifulSoup(driver1.page_source, 'lxml')
Parameters = BSdata1.find_all('table',{'class':'ui-table hp-formTable ui-table_type1 ui-table_sortable'})

它一直选择两个表。如何只选择第一个? 我在Windows计算机上使用Python3,BS4和lxml解析器。

1 个答案:

答案 0 :(得分:0)

您问题中的样本表格式不正确,但是使用css选择器可以正常工作:

const form = document.getElementById("loginForm");
const data = new FormData(form);
axios.post('{{path('login')}}', data);

输出:

driver1.page_source = """
<doc>
<table class="ui-table hp-formTable ui-table_type1 ui-table_sortable">First Table</table>
<table class="ui-table hp-raceRecords ui-table_type2">Second Table</table>
</doc>
"""
BSdata1 = BeautifulSoup(driver1.page_source, 'lxml')
Parameters = BSdata1.select('table.ui-table.hp-formTable.ui-table_type1.ui-table_sortable')
Parameters