iT邦幫忙

0

Python爬取證交所資料製圖出現return self._engine.get_loc(key)問題

  • 分享至 

  • xImage

小弟有個問題,找了一陣子還是找不出問題,也上網找了還是沒有明確答案...
我希望從證交所將每日大盤資訊爬下來

import time
crawl_date = datetime(2019,11,1) # start_date

for i in range(31): 
    crawl_date -= timedelta(1)
    crawl_date_str = datetime.strftime(crawl_date, '%Y%m%d')
    res = requests.get('https://www.twse.com.tw/exchangeReport/BFIAMU?response=json&date=' + crawl_date_str+'&_=1576056207631')
    
    jres = json.loads(res.text)
    
    if(jres['stat']=='OK'):
        print(crawl_date_str, ': crawling data...')
       
        df_temp = pd.DataFrame(jres['data'],columns=jres['fields'])
        
        row_data = list(df_temp['漲跌指數'])
        row_data.append(crawl_date_str)
        df.loc[len(df)] = row_data #用loc指令,把資料(row_data)傳進df指定的位置,df的指定位置就是用len(df),也就是df中row的長度
    else:
        print(crawl_date_str, ': no data')
        
    time.sleep(5) 
df = df.reset_index(drop=True)

在此之前我已先抓一日資料,並以該日資料的名稱建置DataFrame的表頭

import requests
import json
import pandas as pd
res = requests.get('https://www.twse.com.tw/exchangeReport/BFIAMU?response=json&date=20191209&_=1576056207631')
jres = json.loads(res.text)
df_temp = pd.DataFrame(jres['data'],columns=jres['fields'])
column_list = list(df_temp['分類指數名稱'])
column_list.append('date')
df = pd.DataFrame(columns=column_list)

我已把資料型態改了

df = df.astype(float)
df.dtypes

我想畫個圖

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
plt.rcParams['font.sans-serif'] = ['simhei']
plt.rcParams['axes.unicode_minus'] = False
plt.style.use('ggplot')

%matplotlib inline

plt.figure(figsize=(10,8))
plt.plot(df["食品類指數"])

出現以下訊息

KeyError                                  Traceback (most recent call last)
/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2656             try:
-> 2657                 return self._engine.get_loc(key)
   2658             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: '食品類指數'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-29-16cef854be62> in <module>
      1 #df['水泥類指數'].plot(figsize=(10,8))
      2 plt.figure(figsize=(10,8))
----> 3 plt.plot(df["食品類指數"])
      4 #df['食品類指數'].plot(figsize=(10,8))
      5 #df.plot(kind='line',title='折線圖',figsize=(10,8))

/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2925             if self.columns.nlevels > 1:
   2926                 return self._getitem_multilevel(key)
-> 2927             indexer = self.columns.get_loc(key)
   2928             if is_integer(indexer):
   2929                 indexer = [indexer]

/anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2657                 return self._engine.get_loc(key)
   2658             except KeyError:
-> 2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2660         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2661         if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: '食品類指數'

找了好久實在找不出哪裡出錯,請教各位高手,能否協助解答

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

1 個回答

0
listennn08
iT邦高手 5 級 ‧ 2019-12-18 09:30:24
最佳解答

Error 看起來是抓不到 Key
如果你把 dataframe 的 column name 印出來你會發現後面多了空格

'食品類指數          '

你多加這一行就可以了

df.columns = df.columns.str.strip()
chifeng iT邦新手 5 級 ‧ 2019-12-18 14:49:34 檢舉

萬分感謝!

我要發表回答

立即登入回答