参考にした記事
https://qiita.com/ogi-iii/items/cc9a7bc45f7b15001440
発生している問題・エラーメッセージ
IndexError Traceback (most recent call last) <ipython-input-1-5ea6d0fc191f> in <module> 24 #choose a stock out of list 25 stockClick=browser.find_elements_by_class_name("clickable") ---> 26 stockClick[num].find_element_by_tag_name("a").click() 27 28 stockTable=browser.find_element_by_class_name("table_wrap") IndexError: list index out of range
該当のソースコード
python
1#ready for scraping 2columnNames=[] 3ETFComparisonsTable=[] 4for num in range(0,48): 5 browser.get("https://kabuoji3.com/stock/") 6 stockSearch=browser.find_element_by_class_name("form_inputs") 7 stockSearchForm=stockSearch.find_element_by_class_name("form_txt") 8 stockSearchForm.send_keys("ETF") 9 btnClick=browser.find_element_by_class_name("btn_submit") 10 btnClick.click() 11 12 #choose a stock out of list 13 stockClick=browser.find_elements_by_class_name("clickable") 14 stockClick[num].find_element_by_tag_name("a").click() 15 16 stockTable=browser.find_element_by_class_name("table_wrap") 17 stockLine=stockTable.find_elements_by_tag_name("tr") 18 19 #price scraping with calculation 20 if len(stockLine)==302: 21 ETFComparisons=[] 22 for i in range(2,152): 23 stockETFPriceAfter=stockLine[i-1].find_elements_by_tag_name("td") 24 stockETFPriceBefore=stockLine[i].find_elements_by_tag_name("td") 25 ETFComparison=float(stockETFPriceAfter[6].text)-float(stockETFPriceBefore[6].text) 26 ETFComparisons.append(ETFComparison) 27 28 stockETFPriceAfter=stockLine[151].find_elements_by_tag_name("td") 29 stockETFPriceBefore=stockLine[153].find_elements_by_tag_name("td") 30 ETFComparison=float(stockETFPriceAfter[6].text)-float(stockETFPriceBefore[6].text) 31 ETFComparisons.append(ETFComparison) 32 33 for i in range(154,302): 34 stockETFPriceAfter=stockLine[i-1].find_elements_by_tag_name("td") 35 stockETFPriceBefore=stockLine[i].find_elements_by_tag_name("td") 36 ETFComparison=float(stockETFPriceAfter[6].text)-float(stockETFPriceBefore[6].text) 37 ETFComparisons.append(ETFComparison) 38 39 ETFComparisonsTable.append(ETFComparisons) 40 41 #pick up title 42 stockTitleBox=browser.find_element_by_class_name("base_box_ttl") 43 stockTitle=stockTitleBox.find_element_by_class_name("jp").text 44 columnNames.append(stockTitle) 45 46#making ETF table 47ETFTable=pd.DataFrame(ETFComparisonsTable) 48ETFTable=ETFTable.T 49ETFTable.columns=columnNames 50#checking ETF table 51ETF.tail()
補足情報(FW/ツールのバージョンなど)
参考にしたQiitaの記事
https://qiita.com/ogi-iii/items/cc9a7bc45f7b15001440
jupyter lab
回答1件
あなたの回答
tips
プレビュー