Python BeautifulSoup,在不使用find_all()的情况下查找第n个表

Python BeautifulSoup,在不使用find_all()的情况下查找第n个表,python,beautifulsoup,wikipedia,Python,Beautifulsoup,Wikipedia,我想用BeautifulSoup查找第n个表。到目前为止,我一直在做这项工作 table=soup.find_all('table',{'class':'wikitable sortable jquery tablesorter'})[nth] 但是,如果我确定它是我定义的第n个表,有没有办法避免搜索和保存所有以前的表?我觉得如果有一种方法只获取第n个表,那么我的代码就会运行得更快。这些表格来自wikipedia。使用。选择类型为n的。我不确定这是否会使您的代码运行更快,为此,请查看文档的部分

我想用BeautifulSoup查找第n个表。到目前为止,我一直在做这项工作

table=soup.find_all('table',{'class':'wikitable sortable jquery tablesorter'})[nth]


但是,如果我确定它是我定义的第n个表,有没有办法避免搜索和保存所有以前的表?我觉得如果有一种方法只获取第n个表,那么我的代码就会运行得更快。这些表格来自wikipedia。

使用
。选择
类型为
n的
。我不确定这是否会使您的代码运行更快,为此,请查看文档的部分

from bs4 import BeautifulSoup
html="""
<table class="1">
</table>
<table class="2">
</table>
<table class="3">
</table>
<table class="4">
</table>
<table class="5">
</table>
"""
soup=BeautifulSoup(html,'html.parser')
print(soup.select('table:nth-of-type(3)'))
输出

[
我们的表2
]
上述输出也可以通过
soup.select('.parent2.tbl~.tbl')

或者
find()
然后n-1次来完成?
[<table class="3">
</table>]
from bs4 import BeautifulSoup
html="""
<div class="parent1">
<table class="tbl">
not our table 1
</table>
<table class="tbl">
not out table 2
</table>
</div>
<div class="parent2">
<table class="tbl">
our table 1
</table>
<table class="tbl">
our table 2
</table>
</div>
"""
soup=BeautifulSoup(html,'html.parser')
print(soup.select('.parent2 table:nth-of-type(2)'))
[<table class="tbl">
our table 2
</table>]