Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python错误索引器错误:列表索引超出范围_Python - Fatal编程技术网

Python错误索引器错误:列表索引超出范围

Python错误索引器错误:列表索引超出范围,python,Python,错误:回溯最近的调用上次:文件 **************\市场_dumper.py,第19行,in 派系=数据[14];索引器:列表索引超出范围 你正在吃晚饭 错误:回溯最近的调用上次:文件************\market_dumper.py,第19行,在派系=数据[14]中;索引器:列表索引超出范围 因为数据可能不包含15个元素。如果有,这行就不应该抛出错误。尝试printdata并检查它是否有15个元素 另外,请编辑您的帖子,并将打印结果放入帖子中,以便我们可以看到数据中有多少元素

错误:回溯最近的调用上次:文件 **************\市场_dumper.py,第19行,in 派系=数据[14];索引器:列表索引超出范围

你正在吃晚饭

错误:回溯最近的调用上次:文件************\market_dumper.py,第19行,在派系=数据[14]中;索引器:列表索引超出范围

因为数据可能不包含15个元素。如果有,这行就不应该抛出错误。尝试printdata并检查它是否有15个元素


另外,请编辑您的帖子,并将打印结果放入帖子中,以便我们可以看到数据中有多少元素。

数据少于15个元素-打印出来,看看发生了什么。请修复示例代码中的缩进。然后扔掉所有愚蠢的分号。
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()