简体   繁体   English

IndexError:列表索引超出范围(Python 网页抓取)

[英]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.

相关问题 Python 网页抓取“IndexError: list index out of range” - Python web scraping “IndexError: list index out of range” Web抓取Python:IndexError:列表索引超出范围 - Web scraping python: IndexError: list index out of range Web抓取:IndexError:列表索引超出范围 - Web scraping: IndexError: list index out of range 当代码缺失值时,如何修复Web抓取Python代码“ IndexError:列表索引超出范围” - How to fix web scraping Python code “IndexError: list index out of range” when the code hits missing values Python 抓取数据 - IndexError:列表索引超出范围 - Python scraping data - IndexError: list index out of range 刮表:IndexError: list index out of range - Scraping a table: IndexError: list index out of range IndexError:字符串索引超出范围[python,抓取] - IndexError: string index out of range [python, scraping] IndexError:列表索引超出范围(使用 python 的网络爬虫) - IndexError: list index out of range (web scraper using python) Python Web 抓取错误 - 从 JSON 读取 - IndexError:列表索引超出范围 - 我该如何忽略 - Python Web Scraping error - Reading from JSON- IndexError: list index out of range - how do I ignore IndexError:列表索引超出python列表的范围 - IndexError: list index out of range in python list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM