2019年3月19日 星期二

python os#path #write #close time#sleep

import time
import os

#if no directory called andrew_course9
if 'andrew_course9' not in os.listdir():
    #create andrew_course9 directory
    os.mkdir('andrew_course9')
#stock_id   
sid = ['1101','2330']

for s in sid:
    res = requests.get('website')
    res.encoding = 'big5'
    #set up save address , ex:andrew_course9\1101.html
    path = os.path.join('andrew_course9',s+'.html')
    #open the file ,write in ,then close
    f = open(path,'w',encoding='utf-8')
    f.write(res.text)
    f.close()
   
    print(s)
    #pause 20 sec , to prevent website does not let you catch data
    time.sleep(20)

********************************************************************************

dfs = []
#for every stock
for s in sid:
    #get the file from directory
    path = os.path.join('andrew_course9', s + '.html')
   
    dfs.append(pd.read_html(path,encoding = 'utf-8'))
   

dfs[1][1].head(10)

*********************************************************************************

沒有留言:

張貼留言