herrDeng網內搜尋

自訂搜尋

Ads

2024年11月25日 星期一

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

url="https://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date=20220330&stockNo=2330"
print('練習股票爬蟲')
  1. import numpy as np
  2. import requests
  3. import matplotlib.pyplot as plt
  4.  
  5. for i in range(50):
  6. r=requests.get('https://eclass.uch.edu.tw/media/40004/')
  7. print(i, r.status_code)
  8.  
  9.  
  10. url="https://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date=20241031&stockNo=2330"
  11.  
  12. data=requests.get(url)
  13.  
  14. data.text
  15.  
  16. jsonTxt=data.json()
  17. jsonTxt
  18.  
  19. xx=jsonTxt['data']
  20.  
  21. price=[]
  22. days=[]
  23.  
  24. for x in xx:
  25. print(x[0],'-->', x[6])
  26. _, _,day=x[0].split('/')
  27. days.append(int(day))
  28. x[6]=x[6].replace(',', '')
  29. price.append(float(x[6]))
  30.  
  31. plt.bar(days,price)
  32. plt.xlim([1,31.5])
  33. plt.show()
  34.  
  35. plt.plot(days,price, 'r-')
  36. plt.xlim([1,30])
  37. plt.show()

1 則留言:

anwendeng 提到...

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

Related Posts Plugin for WordPress, Blogger...

熱門文章