Last active
August 10, 2020 13:52
-
-
Save memorysaver/1064465f51365ef16df0e6da78a5f9c6 to your computer and use it in GitHub Desktop.
台股股價爬蟲
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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