herrDeng網內搜尋

自訂搜尋

Ads

2024年4月8日 星期一

python requests股市爬蟲與pyplot數據視覺化操作


Python code如下:
print('練習股票爬蟲& Matplotlib.pyplot')

import numpy as np
import requests
import matplotlib.pyplot as plt


for i in range(50):
    r=requests.get('https://eclass.uch.edu.tw/media/33895/')
    print(i, r.status_code)

num=2330 

url="https://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date=20240331&stockNo="+str(num)

data=requests.get(url)

data.text


jsonTxt=data.json()
#jsonTxt

title=jsonTxt['title']
title

xx=jsonTxt['data']

price=[]
dd=[]

for x in xx:
    print(x[0],'-->', x[6])
    dd.append(x[0])
    x[6]=x[6].replace(',', '')
    price.append(float(x[6]))

 

 繪圖部分


fig, ax = plt.subplots(figsize=(8,6))
plt.rcParams['font.family']='Microsoft JhengHei'
ax.set_title(title)
ax.set_xlim(0, len(dd)-1)
ax.set_xticks(range(0, len(dd), 5))
ax.xaxis.set_tick_params(rotation = 45)
ax.grid(which = 'major', axis = 'both')
plt.plot(dd ,price, 'ro--')
plt.savefig('stock.png')
plt.show()

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章