Python 美化页面中具有多个表的SOUP刮削特定表

Python 美化页面中具有多个表的SOUP刮削特定表,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,这是我从页面上为刮表编写的代码“https://en.wikipedia.org/wiki/List_of_multiple_Olympic_gold_medalists" 如果我只是针对“职业生涯中最多奥运会金牌榜”中的表格,我如何指定我需要的表格?有两个可排序的jquery tablesorter,因此我无法使用class属性来选择所需的表 还有一个问题,如果我知道我正在抓取的页面包含很多表,并且我需要的页面在1行中总是有10td,我可以有类似的内容吗 import requests fro

这是我从页面上为刮表编写的代码“https://en.wikipedia.org/wiki/List_of_multiple_Olympic_gold_medalists"

如果我只是针对“职业生涯中最多奥运会金牌榜”中的表格,我如何指定我需要的表格?有两个可排序的
jquery tablesorter
,因此我无法使用class属性来选择所需的表

还有一个问题,如果我知道我正在抓取的页面包含很多表,并且我需要的页面在1
行中总是有10
td
,我可以有类似的内容吗

import requests
from bs4 import BeautifulSoup

results = requests.get("https://en.wikipedia.org/wiki/List_of_multiple_Olympic_gold_medalists")

src = results.content

soup = BeautifulSoup(src, 'lxml')

trs = soup.find_all("tr")
for tr in trs:
    print(tr.text)
提取我想要的数据

代码更新:

If len(td) == 10:
print(tr)
我有一个解决方案,不是一个好的,只是从我需要的页面中提取第一个表,欢迎任何建议/改进


谢谢。

要仅获取第一个表,您可以使用CSS选择器
第n个类型(1)


这将更像是
len(soup.find_all('td'))
谢谢,我正在尝试,如果它有效的话
from bs4 import BeautifulSoup

results = requests.get("https://en.wikipedia.org/wiki/List_of_multiple_Olympic_gold_medalists")

src = results.content

soup = BeautifulSoup(src, 'lxml')

tbs = soup.find("tbody")
trs = tbs.find_all("tr")
for tr in trs:
    print(tr.text)
import requests
from bs4 import BeautifulSoup

URL = "https://en.wikipedia.org/wiki/List_of_multiple_Olympic_gold_medalists"

soup = BeautifulSoup(requests.get(URL).content, "html.parser")

table = soup.select_one("table.wikitable:nth-of-type(1)")
trs = table.find_all("tr")

for tr in trs:
    print(tr.text)