2017-08-18 13 views
-3
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() 

Error : Traceback (most recent call last): File "**************\market_dumper.py", line 19, in faction=data[14]; IndexError: list index out of rangePythonのエラーはIndexErrorは:範囲外のリストインデックス

+5

'data'には15個未満の要素があります - それを印刷して、何が起こっているのかを見てください。 –

+1

サンプルコードでインデントを修正してください。 (そして、すべての愚かなセミコロンを取り除く)。 – ekhumoro

答えて

2

あなたはdata 15個の要素が含まれていない可能性がありますよう

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

を持っています。もしそうなら、この行はエラーを投げてはいけません。 print(data)を試して、要素数が15かどうかを確認してください。

また、投稿を編集してprint()の結果を投稿に入れて、dataに含まれる要素の数を確認してください。

関連する問題