顯示具有 pandas 標籤的文章。 顯示所有文章
顯示具有 pandas 標籤的文章。 顯示所有文章

2020年1月30日 星期四

[程式][投資] 查看近 12 年 EPS 資訊及其年增率


在 這篇 有提到過,希望能讓投資更加穩健,在 理財達人秀 - 存股敵8個 看到了 EPS 的重要,想要可以在找新的股票或是在檢視手中股票時可以方便的查詢 EPS 和年增率,另外也想學習 Python,就寫了一個 script,同時也把程式放在 github 上讓大家方便存取。

透過下面網址連到公開資訊觀測站,輸入公司代號後,就會列出 "簡明綜合損益表(三年)",表格的最下方就有 "基本每股盈餘(元)" 資訊,我是以這個資訊當做例子來製作 script。
https://mops.twse.com.tw/mops/web/t163sb17

想要看三年以前的資料,可以選擇歷史資料和年度即可,但是 101 年以前的資料,網站會指引你去別的網頁查,使用方法跟上面的網頁完全一樣。

利用 Python 送出 4 次 request 取回這些資料後,利用 pandas 把網頁資訊分析後找出 EPS,再把這些 EPS 資料存到變數裡,最後再將每年的年增率算出,然後排成適合閱讀的列表,如上圖。

關於怎麼查看網頁 POST 資料,可以參考 這篇。
這只是個開始,之後會看還需要什麼樣的資訊,繼續寫新的 script。

Reference:




2018年2月25日 星期日

[程式] Pandas 初體驗:找出投資商品配息資料


因為想要用 Python 來協助我更快看股票配息資料,找到了 Pandas 和 [Python] 使用 Pandas 來撈資料,照著上面的做,很快的我可以抓到 stockq 的資料(程式如下)
import pandas as pd
url = 'http://www.stockq.org/market/asia.php'
table = pd.read_html(url)[4]
table = table.drop(table.columns[[0,1,2,3,4]],axis=0)
table = table.drop(table.columns[9:296],axis=1)
print table
我想抓我最常看殖利率的網站 Goodinfo! 台灣股市資訊網,卻得到 error 如下

ValueError: No tables found

用 WHATS MY INFO 確認一下 user-agent 是 Python-urllib/2.7,再用 User-Agent Switcher for Google Chrome 測試一下,果然會得到 "請勿透過網站內容下載軟體查詢本網站" 的回應。

試著把他換成一般 chrome browser 的 user-agent 再試試,果然讀的到東西(程式如下)
import pandas as pd 
import requests
url = 'https://goodinfo.tw/StockInfo/StockDividendPolicy.asp?STOCK_ID=1416'
table = pd.read_html(requests.get(url, headers={'User-agent': 'Mozilla/5.0 
(Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/63.0.3239.132 Safari/537.36'}).text)
print table
後來發現 Yahoo Finance 是比較適合拿資料的地方,因為他可以拿到台港美股的資料,但改成用 Yahoo Finance 後又遇到下面這個錯誤
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U95') dtype('<U95') dtype('<U95')

這個錯誤是跟編碼有關的問題,看起來是 pands.read_html() 裡面產生的問題,之前用 Python 爬東西時,也常遇到這種編碼問題,為了避開這個問題,我們先將資料用 BeautifulSoup 做處理,拉出 table 後,再餵進 pandas,就可以抓出 Yahoo Finance 上面顯示的歷史配息資料了,執行後會看到 Quote:,輸入在 Yahoo Finance 上找的到的代號,就會顯示結果如下了,程式和執行結果如下:

程式:
import pandas as pd 
import requests
from bs4 import BeautifulSoup
x = raw_input("Quote: ")
url = 'https://finance.yahoo.com/quote/'+x+'/history?period1=946915200&period2=1519401600&interval=div%7Csplit&filter=div&frequency=1d'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'lxml')
table = soup.find_all('table')[0];
dfs = pd.read_html(str(table))
for index, row in dfs[0].iterrows():
    print row[0], row[1]

執行結果:
Quote: 2330.tw
Jun 26, 2017 7 Dividend
Jun 27, 2016 6 Dividend
Jun 29, 2015 4.49988 Dividend
Jul 14, 2014 2.99991 Dividend
Jul 03, 2013 2.99955 Dividend
Jul 04, 2012 2.99948 Dividend
Jun 29, 2011 2.99951 Dividend
Jul 06, 2010 2.99973 Dividend
Jul 15, 2009 2.98497 Dividend
Jul 16, 2008 3.00989 Dividend
Jun 08, 2007 2.98459 Dividend
Jun 20, 2006 2.42631 Dividend
Jun 13, 2005 1.90457 Dividend
Jun 14, 2004 0.529159 Dividend

因為這是初體驗,所以沒有任何 error handle 的處理,請小心使用 :)

安裝 library 方法:
pip install pandas
pip install BeautifulSoup
pip install requests

Reference:
Pandas
[Python] 使用 Pandas 來撈資料
Goodinfo! 台灣股市資訊網
WHATS MY INFO
User-Agent Switcher for Google Chrome
BeautifulSoup