Skip to content

Instantly share code, notes, and snippets.

@Wei1234c
Created June 28, 2017 16:53
Show Gist options
  • Save Wei1234c/349957bfced9a2564fc07f0572f2ee52 to your computer and use it in GitHub Desktop.
Save Wei1234c/349957bfced9a2564fc07f0572f2ee52 to your computer and use it in GitHub Desktop.
練習 - 台泥的資產負債表
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import requests
def get_fs_html(stock_no):
url = 'http://goodinfo.tw/StockInfo/StockFinDetail.asp?RPT_CAT=BS_M_QUAR&STOCK_ID=' + stock_no
html = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'})
return html
def get_table(html):
targetTableIndex = 0
table = pd.read_html(html.content,
attrs = {'class': 'solid_1_padding_3_4_tbl',},
header = 0
)[targetTableIndex]
return table
table = get_table(get_fs_html('1101'))
table.tail(2)
@Pacotylin
Copy link

Dear Wei
我多加了一下的code, 不過我想抓合併年表,option value ="BS_M_YEAR",該如何寫?
TKS~

stocklist = ['1101', '2498']
for item in stocklist:
table = get_table(get_fs_html(item))
print (table)
table.to_excel("balance.xls")

@Wei1234c
Copy link
Author

Wei1234c commented Jul 4, 2017

@Pacotylin
Copy link

Dear Wei
再請教一下,我要抓取台泥損益表,default為”合併累計季表“,欲切換到“合併年表“時 url不會變
http://goodinfo.tw/StockInfo/StockFinDetail.asp?RPT_CAT=IS_M_QUAR_ACC&STOCK_ID=1101)
請問該怎麼抓累計年表? Appreciated!

@Wei1234c
Copy link
Author

Wei1234c commented Jul 6, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment