小编Ben*_*ner的帖子

当表无法返回值时,如何抓取表?(美汤)

以下是我的代码:

import numpy as np
import pandas as pd
import requests
from bs4 import BeautifulSoup

stats_page = requests.get('https://www.sports-reference.com/cbb/schools/loyola-il/2020.html')
content = stats_page.content
soup = BeautifulSoup(content, 'html.parser')
table = soup.find(name='table', attrs={'id':'per_poss'})

html_str = str(table)
df = pd.read_html(html_str)[0]
df.head()
Run Code Online (Sandbox Code Playgroud)

我得到错误: ValueError: No tables found.

但是,当我attrs={'id':'per_poss'}使用不同的表 ID 进行交换时 ,就像 attrs={'id':'per_game'}得到输出一样。

我不熟悉 html 和抓取,但我注意到在工作表中,这是 html: <table class="sortable stats_table now_sortable is_sorted" id="per_game" data-cols-to-freeze="2">

在不起作用的表格中,这是 html: <table class="sortable stats_table now_sortable sticky_table re2 le1" id="totals" data-cols-to-freeze="2">

似乎表类不同,我不确定这是否是导致此问题的原因以及如何解决。

谢谢!

html python beautifulsoup web-scraping pandas

5
推荐指数
1
解决办法
76
查看次数

标签 统计

beautifulsoup ×1

html ×1

pandas ×1

python ×1

web-scraping ×1