程式碼: import pandas as pd import requests from io import StringIO import time defmonthly_report(year, month): # 假如是西元,轉成民國if year >1990: year -=1911 url ='https://mops.twse.com.tw/nas/t21/sii/t21sc03_'+str(year)+'_'+str(month)+'_0.html'if year <=98: url ='https://mops.twse.com.tw/nas/t21/sii/t21sc03_'+str(year)+'_'+str(month)+'.html' # 偽瀏覽器 headers ={'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} # 下載該年月的網站,並用pandas轉換成 dataframe r = requests.get(url, headers=headers) r.encoding ='big5' dfs = pd.read_html(StringIO(r.text), encoding='big-5') df = pd.concat([df for df in dfs if df.shape[1]<=11and df.shape[1]>5]) if'levels'indir(df.columns): df.columns = df.columns.get_level_values(1)else: