jeudi 25 juillet 2019

IndexError: list index out of range (Python web scraping)

This is my first time web scraping. I've followed a tutorial but I'm trying to scrape a different page and I'm getting the following:

gamesplayed = data[1].getText()

IndexError: list index out of range

This is the code so far

from bs4 import BeautifulSoup
import urllib.request
import csv

urlpage =  'https://www.espn.com/soccer/standings/_/league/FIFA.WORLD/fifa-world-cup'
page = urllib.request.urlopen(urlpage)
soup = BeautifulSoup(page, 'html.parser')
#print(soup)

table = soup.find('table', attrs={'class': 'Table2__table__wrapper'})
results = table.find_all('tr')
#print('Number of results:', len(results))


rows = []
rows.append(['Group A', 'Games Played', 'Wins', 'Draws', 'Losses', 'Goals For', 'Goals Against', 'Goal Difference', 'Points'])
print(rows)

# loop over results
for result in results:
    # find all columns per result
    data = result.find_all('td')
    # check that columns have data
    if len(data) == 0:
        continue

    # write columns to variables
    groupa = data[0].getText()
    gamesplayed = data[1].getText()
    wins = data[2].getText()
    draws = data[3].getText()
    losses = data[4].getText()
    goalsfor = data[5].getText()
    goalsagainst = data[6].getText()
    goaldifference = data[7].getText()
    point = data[8].getText()

Thank you all wise and knowing gurus!! Luna




Aucun commentaire:

Enregistrer un commentaire