[英]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() gamesplayed = data[1].getText()
IndexError: list index out of range IndexError:列表索引超出范围
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()
The error message is pretty descriptive: you are trying to access an index in a list that does not exist.错误消息非常具有描述性:您正在尝试访问不存在的列表中的索引。
If data
has to contain at least 9 elements (you are accessing index 0 through 8) then you should probably change如果
data
必须包含至少 9 个元素(您正在访问索引 0 到 8),那么您可能应该更改
if len(data) == 0:
continue
to到
if len(data) < 9:
continue
so you can safely skip data
in such a case.所以在这种情况下你可以安全地跳过
data
。
Please have a look at what follows请看看下面的内容
if len(data) == 0:
continue
block below下面块
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
print(len(data))
# Here's where you didn't see that what you scraped was list of list
print(data)
#[['Group A', 'Games Played', 'Wins', 'Draws', 'Losses', 'Goals For', 'Goals Against', 'Goal Difference', 'Points']]
data = data[0]
# 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.