Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python bs4未给出表格_Python_Beautifulsoup_Python Requests - Fatal编程技术网

Python bs4未给出表格

Python bs4未给出表格,python,beautifulsoup,python-requests,Python,Beautifulsoup,Python Requests,我想从中获得每场比赛球队统计数据的表格内容,但我得到了错误 URL = 'https://www.basketball-reference.com/leagues/NBA_2019.html' content = requests.get(URL) soup = BeautifulSoup(content.text, 'html.parser') table = soup.find_all('table', {'class' : 'sortable stats_table now_sortabl

我想从中获得每场比赛球队统计数据的表格内容,但我得到了错误

URL = 'https://www.basketball-reference.com/leagues/NBA_2019.html'
content = requests.get(URL)
soup = BeautifulSoup(content.text, 'html.parser')
table = soup.find_all('table', {'class' : 'sortable stats_table now_sortable'})
rows = table.find_all('td')
for i in rows:
    print(i.get_text())

您想要的表是动态加载的,这意味着当您第一次向页面发出请求时,它不会加载到html中。因此,您正在搜索的表尚不存在


要删除使用javascript的站点,您可以研究使用selenium webdriver和PhantomJS,这篇文章对此有更好的描述–>

实际上您可以使用
pandas.read_html()
,它将以良好的格式读取所有表。它将以列表的形式返回表。因此,您可以使用诸如
print(df[0])
之类的索引作为数据帧访问它

>>>AttributeError: 'NoneType' object has no attribute 'find_all'
这些体育参考网站中的表格(少数除外)在评论中。您需要取出注释,然后用熊猫呈现这些表

import pandas as pd

df = pd.read_html("https://www.basketball-reference.com/leagues/NBA_2019.html")

print(df)
输出:

import requests
from bs4 import BeautifulSoup, Comment
import pandas as pd

headers = {'User-Agent': 
           'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}

page = "https://www.basketball-reference.com/leagues/NBA_2019.html"
pageTree = requests.get(page, headers=headers)
pageSoup = BeautifulSoup(pageTree.content, 'html.parser')
comments = pageSoup.find_all(string=lambda text: isinstance(text, Comment))


for each in comments:
    if 'table' in each and 'id="team-stats-per_game"' in each:
        df = pd.read_html(each, attrs = {'id': 'team-stats-per_game'})[0]

在哪一行?在表格上?向下滚动,大的h2铭文“每场比赛球队统计数据”,但这不是他们要求的表格。它确实存在于本例中的注释中
print (df)
      Rk                     Team   G     MP    FG  ...  STL  BLK   TOV    PF    PTS
0    1.0         Milwaukee Bucks*  82  241.2  43.4  ...  7.5  5.9  13.9  19.6  118.1
1    2.0   Golden State Warriors*  82  241.5  44.0  ...  7.6  6.4  14.3  21.4  117.7
2    3.0     New Orleans Pelicans  82  240.9  43.7  ...  7.4  5.4  14.8  21.1  115.4
3    4.0      Philadelphia 76ers*  82  241.5  41.5  ...  7.4  5.3  14.9  21.3  115.2
4    5.0    Los Angeles Clippers*  82  241.8  41.3  ...  6.8  4.7  14.5  23.3  115.1
5    6.0  Portland Trail Blazers*  82  242.1  42.3  ...  6.7  5.0  13.8  20.4  114.7
6    7.0   Oklahoma City Thunder*  82  242.1  42.6  ...  9.3  5.2  14.0  22.4  114.5
7    8.0         Toronto Raptors*  82  242.4  42.2  ...  8.3  5.3  14.0  21.0  114.4
8    9.0         Sacramento Kings  82  240.6  43.2  ...  8.3  4.4  13.4  21.4  114.2
9   10.0       Washington Wizards  82  243.0  42.1  ...  8.3  4.6  14.1  20.7  114.0
10  11.0         Houston Rockets*  82  241.8  39.2  ...  8.5  4.9  13.3  22.0  113.9
11  12.0            Atlanta Hawks  82  242.1  41.4  ...  8.2  5.1  17.0  23.6  113.3
12  13.0   Minnesota Timberwolves  82  241.8  41.6  ...  8.3  5.0  13.1  20.3  112.5
13  14.0          Boston Celtics*  82  241.2  42.1  ...  8.6  5.3  12.8  20.4  112.4
14  15.0           Brooklyn Nets*  82  243.7  40.3  ...  6.6  4.1  15.1  21.5  112.2
15  16.0       Los Angeles Lakers  82  241.2  42.6  ...  7.5  5.4  15.7  20.7  111.8
16  17.0               Utah Jazz*  82  240.9  40.4  ...  8.1  5.9  15.1  21.1  111.7
17  18.0       San Antonio Spurs*  82  241.5  42.3  ...  6.1  4.7  12.1  18.1  111.7
18  19.0        Charlotte Hornets  82  241.8  40.2  ...  7.2  4.9  12.2  18.9  110.7
19  20.0          Denver Nuggets*  82  240.6  41.9  ...  7.7  4.4  13.4  20.0  110.7
20  21.0         Dallas Mavericks  82  241.2  38.8  ...  6.5  4.3  14.2  20.1  108.9
21  22.0          Indiana Pacers*  82  240.3  41.3  ...  8.7  4.9  13.7  19.4  108.0
22  23.0             Phoenix Suns  82  242.4  40.1  ...  9.0  5.1  15.6  23.6  107.5
23  24.0           Orlando Magic*  82  241.2  40.4  ...  6.6  5.4  13.2  18.6  107.3
24  25.0         Detroit Pistons*  82  242.1  38.8  ...  6.9  4.0  13.8  22.1  107.0
25  26.0               Miami Heat  82  240.6  39.6  ...  7.6  5.5  14.7  20.9  105.7
26  27.0            Chicago Bulls  82  242.7  39.8  ...  7.4  4.3  14.1  20.3  104.9
27  28.0          New York Knicks  82  241.2  38.2  ...  6.8  5.1  14.0  20.9  104.6
28  29.0      Cleveland Cavaliers  82  240.9  38.9  ...  6.5  2.4  13.5  20.0  104.5
29  30.0        Memphis Grizzlies  82  242.4  38.0  ...  8.3  5.5  14.0  22.0  103.5
30   NaN           League Average  82  241.6  41.1  ...  7.6  5.0  14.1  20.9  111.2

[31 rows x 25 columns]