I get the following error when I try to print matchup:
AttributeError: ResultSet object has no attribute 'text'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?
There are several indexes on the page that will print with .text, but when I go to print all of them, it returns the above error. How do I search and only print the elements that containt ".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
'''
CodePudding user response:
As the error says
You're probably treating a list of elements like a single element
You want a loop, or a list-comprehension
matchup = [el.text for el in soup.find_all('td', class_ = 'viHeaderNorm')]
CodePudding user response:
You're treating them as the same element, find_all returns a list, you can iterate through it instead though.
CodePudding user response:
Could just let pandas parse the tables:
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)
Output:
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]