iT邦幫忙

0

使用 beautiful soup find_all() 報錯: lsit out of range

  • 分享至 

  • xImage

一、我想要爬下述網站的資料
https://tw.stock.yahoo.com/q/q?s=
= 後面接的是 tkinter entry get 輸入得到的代碼變數
二、用 while(true) 一直循環爬取網頁資料,每隔一段時間再爬,並將資料一列一列地寫入 excel
三、我在table01 = soup01.find_all(text='成交')[0].parent.parent.parent 報錯: list out of range

****各位大大好!不好意思打擾各位大大了!請問這個問題該如何解決呢?煩請告知 謝謝!

def blink01():
def run01():
while (switch01 == True):

#決定 excel 報表欄位要設定叫什麼名字
df01 = pd.DataFrame(columns=["時間","名字","價格","漲跌"])

#一、先檢查欄位有沒有填,二、再檢查欄位參數填地是否正確
if len(stock01_entry.get()) == 0:
toast01_label.configure(text = '股票四碼欄位沒填')
print('股票四碼欄位沒填')
break

if len(gap01_entry.get()) == 0:
toast01_label.configure(text = '每隔幾秒欄位沒填')
print('每隔幾秒欄位沒填')
break

try:

#二、再檢查欄位參數填地是否正確
fourname01 = str(stock01_entry.get())
gaptime01 = int(gap01_entry.get())
filename01 = str(file01_entry.get())
page01 = requests.get('https://tw.stock.yahoo.com/q/q?s='+fourname01)
soup01 = BeautifulSoup(page01.content, 'lxml')
table01 = soup01.find_all(text='成交')[0].parent.parent.parent
sname01 = table01.select('tr')[1].select('td')[0].text.strip('加到投資組合')
price01 = table01.select('tr')[1].select('td')[2].text
trend01 = table01.select('tr')[1].select('td')[5].text[0:4]

except:
toast01_label.configure(text = '代碼錯誤、沒有這支股票')
print('代碼錯誤、沒有這支股票')
break
#一、有沒有那「 == True 」的差別
#二、 html.parser vs lxml
while(switch01== True):
page01 = requests.get('https://tw.stock.yahoo.com/q/q?s='+fourname01)
soup01 = BeautifulSoup(page01.content, 'lxml')
#到這裡開始報錯 : lsit out of range
table01 = soup01.find_all(text='成交')[0].parent.parent.parent

if(len(table01)>5):
    continue

price01 = table01.select('tr')[1].select('td')[2].text
trend01 = table01.select('tr')[1].select('td')[5].text[0:4]
#將時間、名字、價格、漲跌整理成一列資料
s01 = pd.Series([time.strftime('%Y%m%d %H:%M:%S'),sname01,price01,trend01], index=['時間',"名字","價格",'漲跌'])
df01 = df01.append(s01, ignore_index=True)  

sf = StyleFrame(df01)   
sf.set_column_width_dict(col_width_dict={("時間"): 18,("名字"): 13})  
sf.to_excel(filename01+' 報表.xlsx',right_to_left=False,columns_and_rows_to_freeze='A1',row_to_add_filters=0).save()

#橫向地列印至 cmd ,如果 print(s) 就會直向地列印至 cmd
print ( time.strftime ('%Y%m%d %H:%M:%S'),len(table01),sname01,price01,trend01 )
#訊息標籤顯示資訊
toast01_label.configure(text = s01.to_string())
#每隔多少秒,定時更新做這些事
time.sleep(gaptime01)

if(switch01 == False):break
thread1 = threading.Thread(target=run01)
thread1.start()

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
froce
iT邦大師 1 級 ‧ 2020-12-29 08:24:34
table01 = soup01.find_all(text='成交')[0]

你得先確定find_all有找到結果才行取第1個吧?

我要發表回答

立即登入回答