【问题标题】:I'm trying to scrape a table from a website but I keep getting an IndexError and can't progress我正在尝试从网站上抓取表格,但我不断收到 IndexError 并且无法继续
【发布时间】:2020-12-14 22:32:11
【问题描述】:

我对编码比较陌生,不确定问题出在哪里。

我正在尝试从 LoL 2020 世界锦标赛中为一个班级项目抓取所有玩家统计数据,但我不断收到索引错误,我不知道如何解决它。这是我正在使用的代码:

import pandas as pd
import re
from bs4 import BeautifulSoup as bs

r = requests.get("https://lol.gamepedia.com/2020_Season_World_Championship/Player_Statistics")

table = webpage.select("table.wikitable.sortable.spstats.plainlinks.hoverable-rows.jquery-tablesorter")[0]
print(table)
columns = table.find("thead").find_all("th")
column_names = [c.string for c in columns]

table_rows = table.find("tbody").find_all("tr")
l = []
for tr in table_rows:
    td = tr.find_all('td')
    row = [str(tr.get_text()).strip() for tr in td]
    l.append(row)

df = pd.DataFrame(1, columns = column_names)

print(df.head(5))

返回:

    table = webpage.select("table.wikitable.sortable.spstats.plainlinks.hoverable-rows.jquery-tablesorter")[0]
IndexError: list index out of range

请帮忙!

【问题讨论】:

    标签: python html pandas web-scraping


    【解决方案1】:

    尝试从您的选择器中删除 "jquery-tablesorter" 类名 - 它会在页面呈现期间附加:

    table = webpage.select("table.wikitable.sortable.spstats.plainlinks.hoverable-rows")[0]
    

    【讨论】:

    • 我认为修复了它,但现在我得到一个不同的错误:AttributeError: 'NoneType' object has no attribute 'find_all'
    • @ColePankau 在哪一行?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-26
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 2021-10-07
    • 2021-02-18
    相关资源
    最近更新 更多