作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下是我的代码:
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()
我得到错误:
ValueError: No tables found.
但是,当我交换
attrs={'id':'per_poss'}
使用不同的表 ID,如
attrs={'id':'per_game'}
我得到一个输出。
<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 注释中 <!-- .... -->
.
您可以提取表检查标签是否为 Comment
类型:
import pandas as pd
import requests
from bs4 import BeautifulSoup, Comment
URL = "https://www.sports-reference.com/cbb/schools/loyola-il/2020.html"
soup = BeautifulSoup(requests.get(URL).content, "html.parser")
comments = soup.find_all(text=lambda t: isinstance(t, Comment))
comment_soup = BeautifulSoup(str(comments), "html.parser")
table = comment_soup.select("#div_per_poss")[0]
df = pd.read_html(str(comment_soup))
print(df)
输出:
[ Rk Player G GS MP FG ... AST STL BLK TOV PF PTS
0 1.0 Cameron Krutwig 32 32.0 1001 201 ... 133 39 20 81 45 482
1 2.0 Tate Hall 32 32.0 1052 141 ... 70 47 3 57 56 406
2 3.0 Marquise Kennedy 32 6.0 671 110 ... 43 38 9 37 72 294
3 4.0 Lucas Williamson 32 32.0 967 99 ... 53 49 9 57 64 287
4 5.0 Keith Clemons 24 24.0 758 78 ... 47 29 1 32 50 249
5 6.0 Aher Uguak 32 31.0 768 62 ... 61 15 3 59 56 181
6 7.0 Jalon Pipkins 30 1.0 392 34 ... 12 10 1 17 15 101
7 8.0 Paxson Wojcik 30 1.0 327 25 ... 18 14 0 14 23 61
...
...
关于python - 当表无法返回值时,如何抓取表? (美汤),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64827590/
我正在努力学习 Ruby Koans 以尝试学习 Ruby,到目前为止一切顺利。我已经得到了贪婪的公案,在撰写本文时它是 183。我有一个可行的解决方案,但我觉得我只是拼凑了一堆 if/then 逻辑
我正在尝试创建一个扩展 boost 图形库行为的类。我希望我的类是一个模板,用户提供一个类型(类),用于在每个顶点存储属性。那只是背景。我正在努力创建一个更简洁的 typedef 来定义我的新类。 基
我正在使用 suds 包从网站查询 API,从他们的网站返回的数据如下所示: (1)。谁能告诉我这是什么格式? (2)。如果是这样,解析数据的最简单方法是什么?我已经使用 BeautifulSoup
所以我有一个看起来像这样的 html 文档: Speaker Name: Title of Talk | Subtitle | website.com ... [Other Stuff] Poste
我是一名优秀的程序员,十分优秀!