如何使用.text打印所有元素?

2024-04-24 21:55:57 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试打印匹配时,出现以下错误:

AttributeError:ResultSet对象没有属性“text”。您可能将元素列表视为单个元素。当您打算调用find()时,是否调用了find_all()

页面上有几个索引将使用.text打印,但当我打印所有索引时,它返回上述错误。如何搜索并仅打印包含“.text”的元素

'''

import bs4
from bs4 import BeautifulSoup
import requests
import lxml



vegas_insider = requests.get('https://www.vegasinsider.com/nfl/matchups/', 'r').text
soup = BeautifulSoup(vegas_insider, 'lxml')

# returns 3rd index of class ID
#spread = soup.find_all('td', class_ = 'viSubHeader2 cellBorderL2 headerTextNorm padCenter')[2].text
#closing_line = soup.find_all('td', class_ = 'viCellBg2 cellBorderL1 cellTextNorm padCenter')[2].text
#home_team = soup.find_all('a', class_ = 'tableText')[1].text

matchup = soup.find_all('td', class_ = 'viHeaderNorm').text

'''


Tags: textimport元素错误allfindrequestslxml
3条回答

如果将它们视为同一个元素,那么find_all将返回一个列表,您可以对其进行迭代

可以让pandas解析表:

import pandas as pd

url = 'https://www.vegasinsider.com/nfl/matchups/'

dfs = pd.read_html(url)
for df in dfs:
    if len(df.columns) > 4:
        print(df)

输出:

                  0              1  ...              7          8
0  8:20 PM Game Time         Record  ...  Betting Trend        NaN
1              Teams       Win-Loss  ...            O/U        ATS
2   109 Tampa Bay «  (4-1) (1-1 A)  ...            74%  Under: 50
3   110 Philadelphia  (2-3) (0-2 H)  ...            26%   Cover: 1

[4 rows x 9 columns]
                     0              1  ...              7          8
0    9:30 AM Game Time         Record  ...  Betting Trend        NaN
1                Teams       Win-Loss  ...            O/U        ATS
2            251 Miami  (1-4) (1-2 A)  ...            60%  Under: 43
3  252 Jacksonville «  (0-5) (0-3 H)  ...            40%   Cover: 6

[4 rows x 9 columns]
                     0              1  ...              7          8
0    1:00 PM Game Time         Record  ...  Betting Trend        NaN
1                Teams       Win-Loss  ...            O/U        ATS
2          253 Houston  (1-4) (0-2 A)  ...            66%  Under: 34
3  254 Indianapolis «  (1-4) (0-2 H)  ...            34%  Cover: 17

[4 rows x 9 columns]
                   0              1  ...              7           8
0  1:00 PM Game Time         Record  ...  Betting Trend         NaN
1              Teams       Win-Loss  ...            O/U         ATS
2   255 Green Bay «  (4-1) (2-1 A)  ...            74%  Cover: 4.5
3        256 Chicago  (3-2) (2-0 H)  ...            26%   Under: 38

[4 rows x 9 columns]
                    0              1  ...              7            8
0   1:00 PM Game Time         Record  ...  Betting Trend          NaN
1               Teams       Win-Loss  ...            O/U          ATS
2  257 Kansas City «  (2-3) (1-1 A)  ...            90%  Cover: 11.5
3      258 Washington  (2-3) (1-2 H)  ...            10%    Under: 44

[4 rows x 9 columns]
                   0              1  ...              7           8
0  1:00 PM Game Time         Record  ...  Betting Trend         NaN
1              Teams       Win-Loss  ...            O/U         ATS
2   259 Minnesota «  (2-3) (0-2 A)  ...            43%  Cover: 3.5
3       260 Carolina  (3-2) (2-1 H)  ...            57%    Over: 62

[4 rows x 9 columns]
                   0              1  ...              7          8
0  1:00 PM Game Time         Record  ...  Betting Trend        NaN
1              Teams       Win-Loss  ...            O/U        ATS
2  261 L.A. Chargers  (4-1) (2-0 A)  ...            69%  Under: 40
3   262 Baltimore «  (4-1) (2-0 H)  ...            31%  Cover: 25

[4 rows x 9 columns]
                   0              1  ...              7            8
0  1:00 PM Game Time         Record  ...  Betting Trend          NaN
1              Teams       Win-Loss  ...            O/U          ATS
2  263 Cincinnati «  (3-2) (1-1 A)  ...            54%  Cover: 19.5
3        264 Detroit  (0-5) (0-2 H)  ...            46%    Under: 45

[4 rows x 9 columns]
                   0              1  ...              7            8
0  1:00 PM Game Time         Record  ...  Betting Trend          NaN
1              Teams       Win-Loss  ...            O/U          ATS
2   265 L.A. Rams «  (4-1) (2-0 A)  ...            42%  Cover: 19.5
3    266 N.Y. Giants  (1-4) (0-2 H)  ...            58%     Push: 49

[4 rows x 9 columns]
                   0              1  ...              7          8
0  4:05 PM Game Time         Record  ...  Betting Trend        NaN
1              Teams       Win-Loss  ...            O/U        ATS
2     267 Arizona «  (5-0) (3-0 A)  ...            50%  Cover: 26
3      268 Cleveland  (3-2) (2-0 H)  ...            50%   Over: 51

[4 rows x 9 columns]
                   0              1  ...              7          8
0  4:25 PM Game Time         Record  ...  Betting Trend        NaN
1              Teams       Win-Loss  ...            O/U        ATS
2   269 Las Vegas «  (3-2) (1-1 A)  ...            59%  Cover: 15
3         270 Denver  (3-2) (1-1 H)  ...            41%   Over: 58

[4 rows x 9 columns]
                   0              1  ...              7         8
0  4:25 PM Game Time         Record  ...  Betting Trend       NaN
1              Teams       Win-Loss  ...            O/U       ATS
2      271 Dallas «  (4-1) (1-1 A)  ...            53%  Cover: 2
3    272 New England  (2-3) (0-3 H)  ...            47%  Over: 64

[4 rows x 9 columns]
                   0              1  ...              7           8
0  8:20 PM Game Time         Record  ...  Betting Trend         NaN
1              Teams       Win-Loss  ...            O/U         ATS
2        273 Seattle  (2-3) (2-1 A)  ...            63%  Cover: 2.5
3  274 Pittsburgh «  (2-3) (1-2 H)  ...            37%    Push: 43

[4 rows x 9 columns]
                   0              1  ...              6              7
0  8:15 PM Game Time         Record  ...  Betting Trend  Betting Trend
1              Teams       Win-Loss  ...          Money            O/U
2        275 Buffalo  (4-1) (2-0 A)  ...            87%            73%
3      276 Tennessee  (3-2) (1-1 H)  ...            13%            27%

[4 rows x 8 columns]

正如错误所说

You're probably treating a list of elements like a single element

您想要一个循环或列表

matchup = [el.text for el in soup.find_all('td', class_ = 'viHeaderNorm')]

相关问题 更多 >