使用 Python 从 URL 中提取数据

Extract Data from A URL with Python

提问人:codedbyjosh 提问时间:8/19/2023 最后编辑:Barmarcodedbyjosh 更新时间:8/19/2023 访问量:30

问:

我正在尝试使用 Python 和 BeautifulSoup 从网站中提取数据。我需要的数据在表格中。

我知道如何使用 .但是,此页上有多个具有相同类名的表。通过id选择表的代码是什么。soup.select('table.class_name')

html 是

<table class="stats_table sortable min_width now_sortable sticky_table eq1 re1 le1 is_sorted" id="matchlogs_for" data-cols-to-freeze=",1">

我尝试的代码

team1_link = team_links[0]
data = urlopen(team1_link,)
soup = BeautifulSoup(data, 'html.parser')
table = soup.select('table.stats_table')

有多个类名为“stats_table”的表,所以我得到了错误的数据

python 解析 网页抓取 beautifulsoup

评论

0赞 Barmar 8/19/2023
soup.select('table#matchlogs_for')将按 ID 选择。
0赞 John Gordon 8/19/2023
soup.find(id="matchlogs_for")
0赞 Tim Roberts 8/19/2023
右。制作 CSS 选择器的规则是 dot 指定一个 CSS 类,pound 指定一个标签 id。.#
0赞 codedbyjosh 8/19/2023
谢谢。知道这一点真的很有帮助。我会确保我记得@TimRoberts

答: 暂无答案