使用Beautifulsoup解析表(索引超出范围)

时间:2016-12-19 14:49:32

标签: python python-3.x beautifulsoup

我正在尝试使用此代码解析Wowprogress和Beautifulsoup在我的服务器上的所有魔兽世界播放器

from urllib.request import urlopen
import os

    adresse="http://www.wowprogress.com/gearscore/eu/hyjal/char_rating/next/0#char_rating"

page=urlopen(adresse)

from bs4 import BeautifulSoup
soup=BeautifulSoup(page)

# print (soup.prettify)
all_tables=soup.find_all("table")

bonne_table=soup.find("table",class_="rating ")

place_liste=[]
pseudo=[]
guilde=[]
ilevel=[]

for row in bonne_table.findAll("tr"):
    cells = row.findAll("td")
    print(cells)
    place_liste.append(cells[0].find(text=True))
     pseudo.append(cells[1].find(text=True))
     guilde.append(cells[2].find(text=True))
     ilevel.append(cells[3].find(text=True))

 print(pseudo)
 print(guilde)
 print(ilevel)

os.system("pause")

我收到错误

place_liste.append(cells[0].find(text=True))
IndexError: list index out of range"

我不明白==>每行有4个“项目”,但它给了我这个错误。我显然不明白为什么它说它超出了范围。

1 个答案:

答案 0 :(得分:0)

第一行没有<td>。它改为标题<th>

实施一些条件检查

if len(cells) >= 4: