首页 > 解决方案 > 索引错误:Python

问题描述

我正在尝试运行此代码并返回 IndexError

import pandas as pd
from bs4 import BeautifulSoup as bs
from math import nan

browser = webdriver.Chrome()


class GameData:
    def __init__(self):
        self.score = []
        self.country = []
        self.league = []
        self.game = []

    def append(self, score):
        pass


def get_urls(browser, landing_page):
    browser.get(landing_page)
    urls = [i.get_attribute('href') for i in
            browser.find_elements_by_css_selector(
                '.next-games-date > a:nth-child(1), .next-games-date > a:nth-child(n+3)')]

    return urls


def parse_data(html):
    global league
    df = pd.read_html(html, header=0)[0]
    html = browser.page_source
    soup = bs(html, "lxml")
    scores = [i.select_one('.table-score').text if i.select_one('.table-score') is not None else nan for i in
              soup.select('#table-matches tr:nth-of-type(n+2)')]
    cont = soup.find('div', {'id': 'wrap'})
    content = cont.find('div', {'id': 'col-content'})
    content = content.find('table', {'class': 'table-main'}, {'id': 'table-matches'})
    main = content.find('th', {'class': 'first2 tl'})

    if main is None:
        return None

    count = main.findAll('a')
    country = count[0].text
    game_data = GameData()
    leagues = [i.text for i in soup.select('.first2 > a:last-child')]

    n = 0

    for number, row in enumerate(df.itertuples()):
        if n == 0 or '»' in row[1]:
            league = leagues[n]
            n += 1
        if not isinstance(row[1], str):
            continue
        elif ':' not in row[1]:
            country = row[1].split('»')[0]
            continue
        game_time = row[1]
        print(len(scores[number])) **This is where I find that the index has changed
        print(scores[number])

        game_data.country.append(country)
        game_data.league.append(league)
        game_data.game.append(row[2])
        game_data.score.append(scores[number])

    return game_data


if __name__ == '__main__':

    start_url = "https://www.oddsportal.com/matches/soccer/"
    urls = []
    browser = webdriver.Chrome()
    results = None
    urls = get_urls(browser, start_url)
    urls.insert(0, start_url)

    for number, url in enumerate(urls):
        if number > 0:
            browser.get(url)
        html = browser.page_source
        game_data = parse_data(html)

        if game_data is None:
            continue

        result = pd.DataFrame(game_data.__dict__)

        if results is None:
            results = result
        else:
            results = results.append(result, ignore_index=True)

我不确定我哪里出错了

Traceback (most recent call last):
  File "C:\Users\harsh\AppData\Roaming\JetBrains\PyCharmCE2021.2\scratches\scratch_10.py", line 112, in <module>
    game_data = parse_data(html)
  File "C:\Users\harsh\AppData\Roaming\JetBrains\PyCharmCE2021.2\scratches\scratch_10.py", line 84, in parse_data
    print(scores[number])
IndexError: list index out of range

详细:既然 SO 要求它。

我可以看到这种方式scores是构建的,它不是最好的。我怎样才能从中提取这个值Xpath

Xpath对于scores是_/html/body/div[1]/div/div[2]/div[6]/div[1]/div/div[1]/div[2]/div[1]/div[7]/table/tbody/tr[9]/td[3]

我怎样才能摆脱分数功能并使用它Xpath

如果是这样,理想情况下,我应该对每个列值使用 Xpath

标签: pythondataframeweb-scrapingbeautifulsoupindex-error

解决方案


推荐阅读