2015年2月25日 星期三

[R 學習筆記]使用 R 捉臺灣證交所網站上臺灣50成分股資訊

        這個程式的目標是將臺灣證交所網站上臺灣50成分股資訊捉下來轉成CSV 檔,以利後續處理應用,在臺灣證交所網站上臺灣50成分股資訊頁內如下:
        經過一番研究後程式碼如下:
library(XML)
library(RCurl)
library(httr)
Sys.setlocale(category='LC_ALL', locale='C')
##generate the list of url
url='http://www.twse.com.tw/ch/trading/indices/twco/tai50i.php'
#網頁內有中文字,先以Big5編碼捉取網頁
get_url_parse =htmlParse(url,encoding ='BIG5')
#抓取關鍵的變項,我們需要的變項夾在一個table的class=tb2,裡面<tr>標籤裡面
tablehead <- xpathSApply(get_url_parse, "//tr[@class='tb2']/td", xmlValue)
#將擷取到的關鍵字轉成XP系統內建編碼 CP950
#特別的是經過xpathSApply解析出來的文字編碼,似乎會自動從BIG5變為UTF-8?
tablehead<-iconv(tablehead,"UTF-8","CP950")
#將擷取到的關鍵字轉成容易閱讀的矩陣格式
table <- matrix(tablehead, ncol = 6, byrow = T)
#將 Taiwan 50 成分股寫入 csv 檔內
write.csv(table,file = "TWN50composition.csv" ,row.names = FALSE)
        程式執行所產生的結果如下:
"V1","V2","V3","V4","V5","V6"
"3474","華亞科","9576","5,093,622,000","50.00%","0.83%"
"4938","和碩","9572","2,290,304,935","74.00%","1.03%"
"3481","群創","9572","9,385,527,616","98.00%","1.04%"
"2330","台積電","9576","25,753,417,412","93.00%","25.82%"
"2303","聯電","9576","12,706,314,290","94.00%","1.34%"
"2882","國泰金","8575","12,606,238,526","63.00%","2.67%"
"2357","華碩","9572","752,760,280","95.00%","1.65%"
"1303","南亞","1353","7,930,821,589","72.00%","2.77%"
"2883","開發金","8775","15,172,996,640","93.00%","1.09%"
"1301","台塑","1353","6,365,673,217","77.00%","2.66%"
"2002","中鋼","1757","15,733,113,947","79.00%","2.29%"
"2311","日月光","9576","7,810,454,946","79.00%","1.87%"
"2317","鴻海","2733","14,581,787,562","87.00%","7.64%"
"1402","遠東新","3763","5,247,916,886","75.00%","0.90%"
"2892","第一金","8355","9,259,254,819","79.00%","0.96%"
"2880","華南金","8355","9,327,970,100","75.00%","0.88%"
"2801","彰銀","8355","6,121,980,625","52.00%","0.41%"
"1216","統一","3577","5,463,476,316","85.00%","1.73%"
"1101","台泥","2353","3,692,175,869","87.00%","0.96%"
"1102","亞泥","2353","3,360,379,285","69.00%","0.63%"
"2382","廣達","9572","3,832,574,432","69.00%","1.48%"
"2308","台達電","2737","2,417,141,304","84.00%","2.98%"
"1326","台化","1353","5,861,186,297","75.00%","2.19%"
"2886","兆豐金","8355","12,484,346,574","80.00%","1.75%"
"2891","中信金","8355","15,257,281,448","97.00%","2.16%"
"2325","矽品","9576","3,116,361,139","95.00%","1.16%"
"2105","正新","3357","3,241,414,671","57.00%","0.99%"
"2395","研華","9572","628,272,477","54.00%","0.58%"
"2408","南科","9576","2,396,100,810","15.00%","0.20%"
"2412","中華電","6535","7,757,446,545","49.00%","2.62%"
"2409","友達","9572","9,435,154,398","92.00%","1.01%"
"2207","和泰車","5379","546,179,184","75.00%","1.39%"
"2301","光寶科","2733","2,330,795,827","91.00%","0.58%"
"9904","寶成","3765","2,941,665,922","90.00%","0.86%"
"2912","統一超","5337","1,039,622,256","54.00%","1.00%"
"2354","鴻準","2737","1,357,569,968","78.00%","0.59%"
"2474","可成","2757","750,691,371","96.00%","1.47%"
"3045","台灣大","6575","3,420,832,827","49.00%","1.29%"
view raw gistfile1.txt hosted with ❤ by GitHub

2014年9月8日 星期一

慈禧:開啟現代中國的皇太后

更多有關 慈禧:開啟現代中國的皇太后 的事情
     這本書用一個不同的角度描述了慈禧的一生,其中對於清朝財政的描述,跟課本上所說清朝財政貧困印象還差真多,查了一下才知道,在光緒十二年(1886)之后的十年間,清朝財政收入始終保持在8000萬兩以上,比嘉道年間的4000余萬兩多了一倍。不僅如此,至光緒三十四年(1908),清政府的歲入突破兩億兩關口,這真是太讓人驚訝了,作者在本書雖強調,其所寫皆有依據,但由書中可看出其較偏向慈禧,所述雖有據但立場較偏,可能是作者是一個女性主義者吧,另一點讓我覺得很別扭的,就是其對黃馬掛的描述,其描述成「皇帝穿的黃馬掛」,可能是中英翻讀的落差所造成的吧。

2014年7月26日 星期六

《臨江仙》 明‧楊慎

滾滾長江東逝水, 
浪花淘盡英雄。 
是非成敗轉頭空。 
青山依舊在,幾度夕陽紅。 
白髮漁樵江渚上, 
慣看秋月春風。 
一壺濁酒喜相逢。 
古今多少事,都付笑談中。


【作者簡介】
楊慎(一四八八年至一五五九年;約等同西方文藝復興三巨匠在世之時)明代文學家。字用修,號升庵。新都(今成都)人,
其父為明武宗、世宗時的首輔楊廷和。明武宗正德六年(一五一一年),楊慎錄取為進士的第一名,是蜀中地區(四川)在明朝的唯一一名狀元。明世宗嘉靖三年(一五二四年),因「議大禮」被貶至雲南永昌衛(今保山),居住三十幾年,死於當地。後由明熹宗天啟年間(一六二一至一六二七年),追諡「文憲」。著有《升菴集》。

楊慎博學廣識,《明史》本傳稱其著述豐富,為明代第一。 《明史》稱其:「明世記誦之博,著作之富,推慎為第一。詩文外,雜著至一百餘種,並行於世。」又善彈琵琶。其最具盛名的長篇彈唱敘史之作為《二十一史彈詞》,敘述三代至元及明末歷史,這首《臨江仙》正是《二十一史彈詞》第三段「說秦漢開場詞」,羅貫中《三國演義》開卷前面的一首小詞,也正是這首《臨江仙》。



[Excel]如何創建自定義函數

在 Excel 內建函式中找不到自已想要的函式怎麼辦?事實上我們可以自已建一個,以下是一個簡單的Excel自定義函數示例,用於將民國年轉為西元: Function 民國轉西元(x As Double) As Double     民國轉西元 = x + 1911 End Func...