herrDeng網內搜尋

自訂搜尋

Ads

2022年3月30日 星期三

python requests讀取台積電股票json資訊爬蟲練習

url="https://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date=20220330&stockNo=2330"
print('練習股票爬蟲')
import numpy as np
import requests
import matplotlib.pyplot as plt


url="https://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date=20220330&stockNo=2330"

data=requests.get(url)

data.text

jsonTxt=data.json()
jsonTxt

xx=jsonTxt['data']

price=[]
days=[]

for x in xx:
    print(x[0],'-->', x[6])
    _, _,day=x[0].split('/')
    days.append(int(day))
    x[6]=x[6].replace(',', '')
    price.append(float(x[6]))

plt.bar(days,price)
plt.xlim([1,31.5])
plt.show()

plt.plot(days,price, 'r-')
plt.xlim([1,30])
plt.show()

1 則留言:

anwendeng 提到...

股價超過1000會有','
請多加一行指令
x[6]=x[6].replace(',', '')

Related Posts Plugin for WordPress, Blogger...

熱門文章