iT邦幫忙

0

python 連接mysql不知道程式碼中哪裡出了問題...這卡了我兩天還是找不到解決辦法,真心懇求各位帥哥美女的幫忙

  • 分享至 

  • xImage

https://img.onl/n92Sya
錯誤問題

IndexError Traceback (most recent call last)
in
143 champions(insertornot,date)
144
--> 145 main()
146
147

in main()
141 mycursor.execute(creat)
142
--> 143 champions(insertornot,date)
144
145 main()

in champions(insertornot, date)
59 n = 0
60 rk = 0
---> 61 for item in test[x].select('.champion-index-table__name'):
62 name = item.text
63 rank = test[x].find_all("td", class_="champion-index-table__cell champion-index-table__cell--rank")[rk].text

IndexError: list index out of range

程式碼如下
import requests
from bs4 import BeautifulSoup
import sys
import mysql.connector
mydb = mysql.connector.connect(
host="127.0.0.1",
user="root",
passwd="",
database="opgg"
)
mycursor = mydb.cursor()

def champions(insertornot,date):
tablename = date + "_champions"
res = requests.get("http://tw.op.gg/champion/")
soup = BeautifulSoup(res.text, features="html.parser")
# for champoin in soup.select(".champion-index__champion-item__name"):
# champoins.append(champoin.text)
# 取得所有英雄名字

# print(champoins) #印出所有英雄名字
# result =soup
# TOPname= soup.select("tbody.tabItem.champion-trend-tier-TOP .champion-index-table__name")
# TOPwinrate = soup.select("tbody.tabItem.champion-trend-tier-TOP .champion-index-table__cell champion-index-table__cell--value")
# for item in TOPwinrate:
#    print(item.text)

# print(TOP.select("champion-index-table__cell champion-index-table__cell--rank")[0].text)

# for chapname in soup.select(".tabItem champion-trend-tier-TOP"):
#    print(chapname.select(".champion-index-table__name selectorgadget_selecteds").text)

soup = BeautifulSoup(res.text, features="html.parser")
test = soup.findAll('tbody')

# w =test[0].select('.champion-index-table__name')
# print(test[1].text)
# print(test[0].find_all("td", class_="champion-index-table__cell champion-index-table__cell--value")[0].text)
# test1 = test[0].find_all("td", class_="champion-index-table__cell champion-index-table__cell--value")[0].text
# test1 = test1.replace('%','')
# test2 = float(test1)
# print(test2)
# for item in test[0].find_all("td", class_="champion-index-table__cell champion-index-table__cell--value")[1].text:
#    print(item.text)

def insertchampions(rank, name, win, pick, position):
    sql = "INSERT INTO " + tablename + " (rank , name, winrate ,pickrate ,position) VALUES (%s, %s ,%s ,%s,%s)"
    val = (rank, name, win, pick, position)
    mycursor.execute(sql, val)
    mydb.commit()

position = ['Top', 'Jungle', 'Middle', 'Bottom', 'Support']

for x in range(0, 5):
    print(position[x])
    n = 0
    rk = 0
    for item in test[x].select('.champion-index-table__name'):
        name = item.text
        rank = test[x].find_all("td", class_="champion-index-table__cell champion-index-table__cell--rank")[rk].text
        rk += 1
        win = test[x].find_all("td", class_="champion-index-table__cell champion-index-table__cell--value")[n].text
        win = win.replace('%', '')
        win = float(win)
        n += 1
        pick = test[x].find_all("td", class_="champion-index-table__cell champion-index-table__cell--value")[n].text
        n += 2
        pick = pick.replace('%', '')
        pick = float(pick)
        if insertornot == 'y':
            insertchampions(rank, name, win, pick, position[x])
            url="http://www.op.gg/champion/"+name+"/statistics/"+position[x]+"/matchup?"
            counter(name,url ,insertornot,position[x],date)
        else:
            pass
        #print(item + win + pick)
        print("名字:{name} 排名:{rank} 勝率:{win} 選用率:{pick}".format(name=name, rank=rank, win=win, pick=pick))
        # print('{name} is {age} years old!'.format(name = 'Justin', age = 35))
        

def counter(champoin,url,insertornot,position,date):
tablename = date + "_counters"

def insertcounter(name, cname, cwin, crate,times, position):
    sql = "INSERT INTO " + tablename + " (name , cname, cwin ,crate,times ,position) VALUES (%s, %s ,%s, %s ,%s,%s)"
    # (name VARCHAR(25) ,cname VARCHAR(25) , winrate FLOAT(20) ,cwin FLOAT(20),crate FLOAT(20),position VARCHAR(10),date INT(10) NOT NULL DEFAULT
    val = (champoin, cname, cwin, crate,times, position)
    mycursor.execute(sql, val)
    mydb.commit()
cookies = dict(customLocale='en_US')
res = requests.get(url, cookies=cookies)
soup = BeautifulSoup(res.text, features="html.parser")
times = soup.select('.champion-matchup-champion-list small')  # 對線次數
nwr = soup.select('.champion-matchup-champion-list span')  # 名字勝率機率

# print(test)
t = 0  # t是次數
n = 0  # n是名字、勝率、機率
# print(nwr[3].text)
for item in times:

    counter = times[t].text
    counter = counter.replace(',', '')
    counter = int(counter)
    t += 1

    cname = nwr[n].text
    n += 1

    cwin = nwr[n].text
    cwin = str(cwin).strip()
    cwin = cwin.replace('%', '')
    cwin = float(cwin)
    n += 1

    crate = nwr[n].text
    crate = str(crate)
    crate = crate.replace('%', '')
    crate = float(crate)

    n += 1
    #print("名字:{cname} 勝率:{cwin} 次數:{counter} 機率:{crate}".format(cname=cname, cwin=cwin, counter=counter, crate=crate))
    if insertornot =='y':
        insertcounter(champoin, cname, cwin, crate,counter, position);

def main():

insertornot = input('是否存入資料庫(y/whatever):')
date = input('輸入今天日期(Ex:20181225):')
if insertornot == 'y':

    tablename = date+"_champions"
    creat="CREATE TABLE "+ tablename + " (rank INT(10), name VARCHAR(25) , winrate FLOAT(20) ,pickrate FLOAT(20),position VARCHAR(10),date INT(10) NOT NULL DEFAULT " + date +")"
    mycursor.execute(creat)
    tablename = date+"_counters"
    creat="CREATE TABLE "+ tablename + " (name VARCHAR(25) ,cname VARCHAR(25)  ,cwin FLOAT(20),crate FLOAT(20),times INT(10),position VARCHAR(10),date INT(10) NOT NULL DEFAULT " + date +")"
    mycursor.execute(creat)

champions(insertornot,date)

main()

froce iT邦大師 1 級 ‧ 2022-06-14 11:27:07 檢舉
IndexError: list index out of range 是指你正在存取你list裡面沒有的index

先把test印出來看一下,確認裡面有東西,並且一共有5個再說。
因為你用下面這迴圈對test從0到4取了5次...

for x in range(0, 5)
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
海綿寶寶
iT邦大神 1 級 ‧ 2022-06-13 17:04:21

先把
res = requests.get("http://tw.op.gg/champion/")
改成
res = requests.get("http://tw.op.gg/champions/")
看看

如果目前的程式碼看了兩天還找不到錯誤
我的想法是
https://ithelp.ithome.com.tw/upload/images/20220613/20001787jgMHrYWiYs.png

我要發表回答

立即登入回答