抓 上市股票 營收月報資料 範例
資料來源
https://mops.twse.com.tw/nas/t21/sii/t21sc03_110_6_0.html
https://mops.twse.com.tw/nas/t21/otc/t21sc03_110_5_0.html
---------------------------------------------------------------------------------------------
import datetime
import json
import os
import re
import pandas as pd
from datetime import datetime
import requests
from pandas import json_normalize
resp = requests.get('https://mops.twse.com.tw/server-java/FileDownLoad?step=9&functionName=show_file&filePath=%2Fhome%2Fhtml%2Fnas%2Ft21%2Fsii%2F&fileName=t21sc03_110_5.csv')
resp.encoding = 'utf-8' #變成中文 之前講過 'big5' 不行就用 'utf-8'
#print(resp.text)
#resp.ok
lines = resp.text.replace('\r', '').split('\n')
import pandas as pd
import numpy as np
from io import StringIO
df = pd.read_csv(StringIO("\n".join(lines[1:])),encoding="utf-8", header=None)
df.columns = ['出表日期','資料年月','公司代號','公司名稱','產業別','當月營收','上月營收','去年當月營收','上月比較增減','去年同月增減','當月累計營收','去年累計營收','前期比較增減','備註']
#將全部 NaN 的值都轉為 0
df = df.replace(np.nan, 0)
df
---------------------------------------------------------------------------------------------
抓 上櫃股票 營收月報資料---------------------------------------------------------------------------------------------
import datetime
import json
import os
import re
import pandas as pd
from datetime import datetime
import requests
from pandas import json_normalize
resp = requests.get('https://mops.twse.com.tw/server-java/FileDownLoad?step=9&functionName=show_file&filePath=%2Fhome%2Fhtml%2Fnas%2Ft21%2Fotc%2F&fileName=t21sc03_110_5.csv')
resp.encoding = 'utf-8' #變成中文 之前講過 'big5' 不行就用 'utf-8'
#print(resp.text)
#resp.ok
lines = resp.text.replace('\r', '').split('\n')
import pandas as pd
import numpy as np
from io import StringIO
df = pd.read_csv(StringIO("\n".join(lines[1:])),encoding="utf-8", header=None)
df.columns = ['出表日期','資料年月','公司代號','公司名稱','產業別','當月營收','上月營收','去年當月營收','上月比較增減','去年同月增減','當月累計營收','去年累計營收','前期比較增減','備註']
#將全部 NaN 的值都轉為 0
df = df.replace(np.nan, 0)
df
---------------------------------------------------------------------------------------------
|