首页 > 解决方案 > 使用 BSoup 解析来自 Python 请求的注释数据

问题描述

我正在尝试解析来自篮球参考的一些数据,但到目前为止我无法这样做。这是我获取原始 html 数据的代码

import requests
from bs4 import BeautifulSoup

url='https://www.basketball-reference.com/teams/DAL/2021/lineups/'
response=requests.get(url=url)
soup=BeautifulSoup(response.content,'html.parser')

>>soup.find(attrs={'id':"all_lineups_5-man_"}).find('table')

最后一行给出了一个错误,但它不应该。我的猜测是它正在发生,因为下<!--图中以黄色突出显示。所以我的问题是,我应该如何处理这个问题?

在此处输入图像描述

标签: python-3.xbeautifulsouppython-requests

解决方案


您可以循环浏览评论,获取表格,然后使用pandas.

例如:

import pandas as pd

import requests
from bs4 import BeautifulSoup, Comment
from tabulate import tabulate

url = 'https://www.basketball-reference.com/teams/DAL/2021/lineups/'
response = requests.get(url)

soup = BeautifulSoup(
    response.content, 'html.parser'
).find_all(text=lambda text: isinstance(text, Comment))

tables = [c for c in soup if "<div" in c]

frames = [pd.read_html(table, flavor="bs4") for table in tables]

print(tabulate(pd.concat(frames[-1])))

pd.concat(frames[-1]).to_csv("table_4.csv", index=False)

输出:

--  ---  -------------------------------  ------  -----  ----  ----  ------  ----  ----  ------  ------  -----  ----  ------  ----  -----  ----  -----  ----  -----  ----  ----  ----  ----  ----
 0    1  L. Dončić | T. Hardaway          435:01    1.9   1.8  -0.5   0.023   0.3   2.1  -0.012   0.025   -2    -3     0.012  -2.1   -3.9  -0.5   -3.9  -1.5   -3.2   0.8  -0.4   1.2  -1.1  -0.9
 1    2  W. Cauley-Stein | L. Dončić      288:50    8.3   5.2   0.4   0.057   0.7  -1.3   0.032   0.06    -2.9  -3.8  -0.003  -0.6    1.2   5.3    1.2   2.5    5.5   0.6  -1.6   3     0.5  -0.8
 2    3  L. Dončić | J. Richardson        266:22   -1.5   0     4.2  -0.023  -0.5   6.1  -0.071  -0.029   -1.1  -1    -0.01   -0.7   -3    -3.7   -3    -2.2   -5    -1.4   2.7   0.8  -4.4  -1.1
 3    4  D. Finney-Smith | J. Richardson  252:38   -3.3  -0.5   5.1  -0.034  -0.4   5.8  -0.06   -0.04    -1.8  -2    -0.012   0.2   -1    -3.4   -1    -1.5   -3.5   0.3   0.2   0    -3.9  -1.7
 4    5  T. Burke | L. Dončić             251:51   -2.2   3.2   1.1   0.03   -1.4   1.6  -0.052   0.021   -7.2  -9.5  -0.008  -3.8   -7    -1.4   -7    -2.8   -6.1  -3.6   0.3   1    -2.5   1.6
 5    6  J. Brunson | T. Hardaway         246:37   -4.4  -2.9  -3.9  -0.012  -3.2  -3.8  -0.05   -0.027    4.5   3.7   0.06   -3.8   -8.1  -2.5   -8.1  -3.2   -7.2  -0.6  -1.2   0    -1.2  -1.6
 6    7  T. Burke | J. Johnson            242:07   -2.5   2.3  -0.6   0.029   0.2   0.2   0.004   0.03    -7.2  -8.5  -0.042  -3.6   -7.1  -2.1   -7.1  -2.8   -6.2  -2.1   0     1.9   0     2.8
 7    8  L. Dončić | D. Finney-Smith      236:07   -3.5   1.6   6.7  -0.019  -0.2   5.3  -0.055  -0.026   -6.4  -6.4  -0.064  -0.4   -2    -2.7   -2    -1.6   -3.6   0.2   0.9  -0.4  -3.1  -0.3
 8    9  T. Hardaway | J. Richardson      230:38   -8.2  -0.7   7.3  -0.048  -0.8   8.8  -0.103  -0.059   -6    -5.8  -0.069   0.2   -2.7  -7.2   -2.7  -3.6   -8.3  -1.1   1     0.7  -5.3   0.1
 9   10  L. Dončić | J. Johnson           229:11   -7.7   2.1  -1     0.028  -1.9  -1.8  -0.035   0.018  -10    -9.7  -0.11   -4.7   -9.1  -3     -9.1  -3.7   -7.8  -2.8  -0.9   0.8  -0.3   2.1
10   11  D. Finney-Smith | T. Hardaway    225:53   -1     3.8  10.2  -0.011   0     4.5  -0.04   -0.02    -8.6  -8.8  -0.077   0.9   -0.6  -5.2   -0.6  -2.2   -5.1   2.3   0.7  -0.9  -5.1  -0.1
11   12  W. Cauley-Stein | T. Hardaway    215:03   12.3   6    -0.9   0.071   1.8  -4.1   0.085   0.082   -1.4  -3.8   0.056  -1.9   -1.2   4.4   -1.2   1.6    3.4   3.9  -1.3   2.7  -0.4   0.1
12   13  T. Hardaway | J. Johnson         214:53   -2.7   0.3  -4.9   0.029  -0.1  -6.5   0.06    0.033   -3.1  -3.6  -0.017  -4.9   -9.3  -2     -9.3  -3.5   -7.8  -2    -0.2   1.2   1     2.1
13   14  J. Brunson | J. Johnson          190:02  -11.5  -1.6  -3.6   0.001  -4.7  -8.1  -0.052  -0.024   -3.7  -2.3  -0.081  -4.7   -9.6  -3.4   -9.6  -4     -8.7  -2.3  -1.9   0.5   0.5   1.8
14   15  T. Hardaway | K. Porziņģis       188:30  -12.7  -6.6  -3.2  -0.055  -1.1   4.8  -0.074  -0.059    1.7  -2.9   0.192  -3.2   -7.3  -5     -7.3  -4.5   -9    -0.6  -3.2   0.1   0.8  -1.2
15   16  L. Dončić | K. Porziņģis         181:14   -8.8  -2.5  -2.1  -0.017  -2.6   3.3  -0.1    -0.03    -1.1  -6.7   0.19   -2.5   -4.9  -1.7   -4.9  -2.6   -5.4   0.2  -4.1   1.5   1.8   0
16   17  T. Hardaway | D. Powell          178:57   -3.2  -0.7   2.1  -0.019   2     3.1   0.025  -0.009   -3.9  -3    -0.071  -2.4   -6.7  -6.1   -6.7  -4.3   -9.7  -0.7   1.2   0.3  -1.5  -0.1
17   18  T. Burke | T. Hardaway           177:36   -2.2  -2.4  -3.7  -0.008   2     2.8   0.027   0.006    0.5  -0.9   0.047  -3.3   -6    -1.8   -6    -3     -6.2  -6.2   2     1.3   0.7   0.1
18   19  J. Brunson | L. Dončić           165:52   -3.6  -0.8  -7.2   0.029  -4.4  -5.7  -0.077   0.008    2.5   3.1   0      -6.2  -11.6  -0.7  -11.6  -3.5   -7.8   0.2  -1.4   1.8   1.1  -1.6
19   20  L. Dončić | D. Powell            162:30   -5     0.3   2.3  -0.009   1     3    -0.002  -0.005   -6.6  -4.3  -0.119  -4.3  -11.1  -7.3  -11.1  -5.8  -13.2   0.7   2.3   0.7  -4.3   0.2
20  nan  Team Average                     965:54   -2.1   0.4   0.6   0.002  -0.7   0.8  -0.025  -0.002   -2.2  -2.3  -0.021  -2.1   -4.7  -2.3   -4.7  -2.2   -4.9  -0.4  -0.6   0.5  -1.8  -0.3
--  ---  -------------------------------  ------  -----  ----  ----  ------  ----  ----  ------  ------  -----  ----  ------  ----  -----  ----  -----  ----  -----  ----  ----  ----  ----  ----

文件的示例输出.csv

在此处输入图像描述


推荐阅读