Skip to content

Instantly share code, notes, and snippets.

@memorysaver
Last active August 10, 2020 13:52
Show Gist options
  • Select an option

  • Save memorysaver/1064465f51365ef16df0e6da78a5f9c6 to your computer and use it in GitHub Desktop.

Select an option

Save memorysaver/1064465f51365ef16df0e6da78a5f9c6 to your computer and use it in GitHub Desktop.
台股股價爬蟲
def price_twe(date):
date_str = date.strftime('%Y%m%d')
res = requests_get('https://www.twse.com.tw/exchangeReport/MI_INDEX?response=csv&date='+date_str+'&type=ALLBUT0999', headers=headers, )
if res.text == '':
print('holiday')
return pd.DataFrame()
price_row = 200
if date >= datetime.date(2020, 8, 10):
price_row = 204
print(f'current price row is at {price_row}')
header = np.where(list(map(lambda l: '證券代號' in l, res.text.split('\n')[:price_row])))[0][0]
df = pd.read_csv(StringIO(res.text.replace('=','')), header=header-1)
df = combine_index(df, '證券代號', '證券名稱')
df = preprocess(df, date)
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment