iT邦幫忙

0

股票即時資訊

我在網路上找了爬取股票即時資訊的方法 但結果執行時表格卻出不來
請問是否有解決的方法 或者有沒有其他更好的爬取方式
現在使用的是Eclipse python3.9
https://ithelp.ithome.com.tw/upload/images/20210615/20137281Kbk6uVRMIw.pnghttps://ithelp.ithome.com.tw/upload/images/20210615/20137281Zk9gOqflbw.pnghttps://ithelp.ithome.com.tw/upload/images/20210615/20137281YBirImlQ1d.png
這是範例給的結果
https://ithelp.ithome.com.tw/upload/images/20210615/201372812lo1nsfNDl.png

haward79 iT邦研究生 3 級 ‧ 2021-06-15 10:51:54 檢舉
程式請貼文字
# !/usr/bin/env python
# coding=utf-8
from IPython.display import display,clear_output
from urllib.request import urlopen
import pandas as pd
pd.options.mode.chained_assignment = None # default='warn'
import datetime
import requests
import sched
import time
import json

s = sched.scheduler(time.time, time.sleep)

def tableColor(val):
if val > 0:
color = 'red'
elif val < 0:
color = 'green'
else:
color = 'white'
return 'color: %s' % color
def stock_crawler(targets):

clear_output(wait=True)


stock_list = '|'.join('tse_{}.tw'.format(target) for target in targets)


query_url = "http://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch="+ stock_list
data = json.loads(urlopen(query_url).read())


columns = ['c','n','z','tv','v','o','h','l','y']
df = pd.DataFrame(data['msgArray'], columns=columns)
df.columns = ['股票代號','公司簡稱','當盤成交價','當盤成交量','累積成交量','開盤價','最高價','最低價','昨收價']
df.insert(9, "漲跌百分比", 0.0)


for x in range(len(df.index)):
if df['當盤成交價'].iloc[x] != '-':
df.iloc[x, [2,3,4,5,6,7,8]] = df.iloc[x, [2,3,4,5,6,7,8]].astype(float)
df['漲跌百分比'].iloc[x] = (df['當盤成交價'].iloc[x] - df['昨收價'].iloc[x])/df['昨收價'].iloc[x] * 100


time = datetime.datetime.now()
print("更新時間:" + str(time.hour)+":"+str(time.minute))


df = df.style.applymap(tableColor, subset=['漲跌百分比'])
display(df)

start_time = datetime.datetime.strptime(str(time.date())+'9:30', '%Y-%m-%d%H:%M')
end_time = datetime.datetime.strptime(str(time.date())+'13:30', '%Y-%m-%d%H:%M')


if time >= start_time and time <= end_time:
s.enter(1, 0, stock_crawler, argument=(targets,))


stock_list = ['1101','1102','1103','2330']


s.enter(1, 0, stock_crawler, argument=(stock_list,))
s.run()
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
0
海綿寶寶
iT邦大神 1 級 ‧ 2021-06-15 08:55:59
最佳解答

先改一列試試看
把 http 改為https

query_url = "http://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch="+ stock_list

改成

query_url = "https://mis.twse.com.tw/stock/api/getStockInfo.jsp?ex_ch="+ stock_list

https://ithelp.ithome.com.tw/upload/images/20210615/20137281xrdm9mSyZ9.png

她會變成不斷的跳一樣的東西

0
stowncc
iT邦新手 5 級 ‧ 2021-06-16 08:57:39

1.安裝Jupyter

pip install jupyter

2.開啟Jupyter Notebook

Jupyter Notebook

3.把你的程式存成.ipynb檔

4.在開啟的網頁上選Upload
https://ithelp.ithome.com.tw/upload/images/20210616/20125929EojzpAkorR.jpg

5.之後再選擇剛才存的ipynb檔

6.在這裡一直點Run
https://ithelp.ithome.com.tw/upload/images/20210616/20125929pTiynFyJwi.jpg

7.最後就會跑出來了
https://ithelp.ithome.com.tw/upload/images/20210616/20125929yLNHVZzkV5.jpg

0
ifurther
iT邦新手 4 級 ‧ 2021-06-18 22:26:51

從需要IPython的套件,就可以知道這個程式碼至少需要使用IPython或是如同上面的回答使用jupyter notebook/lab...,所以要能夠執行的話可能要思考是要改程式碼或是改用上述的套件執行,修改的話可能不能用df.style.applymap,看起來是因為Python的文字模式好像並沒有支援。

我要發表回答

立即登入回答