首页 文章

Python Error IndexError:列表索引超出范围[关闭]

提问于
浏览
-1
import requests, sys
from bs4 import BeautifulSoup

r = requests.get("https://www.crossoutdb.com/");

s=BeautifulSoup(r.content, 'lxml' );

it=s.find(id="ItemTable").find("tbody");

f=open("market.csv", "w")

f.write("item,faction,type,popularity,sell_price,sell_offers,buy_price,buy_ordes,margin\n")

 for item in it.find_all("tr"):
 title=item.find(class_="item-title ").text.strip();

   print(data);
   data=item.contents[1].text.strip().split("\n")
   title=data[0];
   faction=data[1];
   type=data[2];

            rareity=item.contents[3].text.strip()
            popularity = item.contents[5].text.strip();
            sell_price = item.contents[7].text.strip();
            sell_offers = item.contents[9].text.strip();
            buy_price = item.contents[11].text.strip();
            buy_orders= item.contents[13].text.strip();
            margin = item.contents[15].text.strip();

x="\"{}\",{},{},{},{},{},{},{},{}\n".format( title, faction, type,popularity, sell_price, sell_offers, buy_price, buy_orders, margin);
f.write(x)
f.close()

错误:回溯(最近一次调用最后一次):文件“************** \ market_dumper.py”,第19行,在faction = data [14]; IndexError:列表索引超出范围

1 回答

  • 2

    你有

    Error : Traceback (most recent call last): File "**************\market_dumper.py", line 19, in faction=data[14]; IndexError: list index out of range

    因为 data 可能不包含15个元素 . 如果是这样,该行不应该抛出错误 . 尝试 print(data) 并检查它是否有15个元素 .

    此外,请编辑您的帖子并将 print() 的结果放在您的帖子中,以便我们可以查看 data 中有多少元素 .

相关问题