2019年4月8日 星期一

樹莓派安裝 twstock 套件

今天嘗試在鄉下的樹莓派 Pi3 上安裝台股資料擷取套件 twstock, 以前只在 PC 上安裝測試過都很順利, 參考 :

Python 學習筆記 : 台股資料擷取模組 twstock 測試 (一)
Python 學習筆記 : 台股資料擷取模組 twstock 測試 (二)
Python 學習筆記 : 台股資料擷取模組 twstock 測試 (三)

twstock 目前是 1.2.1 版, 我下載 ARM 版 whl 檔後放在 Dropbox 備份 :

https://www.dropbox.com/s/6v0v5uhdv0o7u29/twstock-1.2.1-py3-none-any.whl?dl=0

安裝過程出乎意料非常順利, 只要用 pip3 安裝即可 :


1. 安裝 twstock (成功) :

pi@raspberrypi:~ $ pip3 install twstock 
Collecting twstock
  Downloading https://files.pythonhosted.org/packages/a2/82/5c37e9cf30322845a3a39e05d01f8300adcec27a32095865637b486beacd/twstock-1.2.1-py3-none-any.whl (1.9MB)

Collecting requests (from twstock)
  Downloading https://files.pythonhosted.org/packages/7d/e3/20f3d364d6c8e5d2353c72a67778eb189176f08e873c9900e10c0287b84b/requests-2.21.0-py2.py3-none-any.whl (57kB)

Collecting chardet<3.1.0,>=3.0.2 (from requests->twstock)
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)

Collecting urllib3<1.25,>=1.21.1 (from requests->twstock)
  Downloading https://files.pythonhosted.org/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl (118kB)

Collecting certifi>=2017.4.17 (from requests->twstock)
  Downloading https://files.pythonhosted.org/packages/60/75/f692a584e85b7eaba0e03827b3d51f45f571c2e793dd731e598828d380aa/certifi-2019.3.9-py2.py3-none-any.whl (158kB)

Collecting idna<2.9,>=2.5 (from requests->twstock)
  Downloading https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl (58kB)

Installing collected packages: chardet, urllib3, certifi, idna, requests, twstock
Successfully installed certifi-2019.3.9 chardet-3.0.4 idna-2.8 requests-2.21.0 twstock-1.2.1 urllib3-1.24.1

可見 twstock 有五個相依模組 : chardet, urllib3, certifi, idna, requests.

但匯入 twstock 卻出現錯誤 : 

pi@raspberrypi:~ $ python3   
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from twstock import Stock    
Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.5/site-packages/twstock/stock.py", line 15, in <module>
    from .codes import codes
  File "/home/pi/.local/lib/python3.5/site-packages/twstock/codes/__init__.py", line 1, in <module>
    from .fetch import __update_codes
  File "/home/pi/.local/lib/python3.5/site-packages/twstock/codes/fetch.py", line 14, in <module>
    from lxml import etree
ImportError: No module named 'lxml'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pi/.local/lib/python3.5/site-packages/twstock/__init__.py", line 3, in <module>
    from twstock import stock
  File "/home/pi/.local/lib/python3.5/site-packages/twstock/stock.py", line 17, in <module>
    import analytics
ImportError: No module named 'analytics'

可見還缺 lxml 與 analytics 這兩個套件, 先安裝 analytics :


2. 安裝 analytics (成功) :

pi@raspberrypi:~ $ pip3 install analytics    
Collecting analytics
Collecting redis>=2.7.2 (from analytics)

Collecting nydus>=0.10.6 (from analytics)
Collecting python-dateutil==1.5 (from analytics)

Installing collected packages: redis, nydus, python-dateutil, analytics
Successfully installed analytics-0.6.5 nydus-0.11.0 python-dateutil-1.5 redis-3.2.1  

可見 analytics 又還有 4 個相依套件.


3. 用 pip3 安裝 lxml (失敗) :

接下來補安裝 lxml, 結果失敗 :

pi@raspberrypi:~ $ pip3 install lxml
Collecting lxml
  Downloading https://files.pythonhosted.org/packages/7d/29/174d70f303016c58bd790c6c86e6e86a9d18239fac314d55a9b7be501943/lxml-4.3.3.tar.gz (4.4MB)
Building wheels for collected packages: lxml
  Running setup.py bdist_wheel for lxml ... error
  Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-6h5wkzuy/lxml/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmpoy7tokj1pip-wheel- --python-tag cp35:
  Building lxml version 4.3.3.
  Building without Cython.
  ERROR: b'/bin/sh: 1: xslt-config: not found\n'
  ** make sure the development packages of libxml2 and libxslt are installed **
  Using build configuration of libxslt
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-armv7l-3.5
  creating build/lib.linux-armv7l-3.5/lxml
  ....... (略) .......
  building 'lxml.etree' extension
  creating build/temp.linux-armv7l-3.5
  creating build/temp.linux-armv7l-3.5/src
  creating build/temp.linux-armv7l-3.5/src/lxml
  arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fdebug-prefix-map=/build/python3.5-6waWnr/python3.5-3.5.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -DCYTHON_CLINE_IN_TRACEBACK=0 -Isrc -Isrc/lxml/includes -I/usr/include/python3.5m -c src/lxml/etree.c -o build/temp.linux-armv7l-3.5/src/lxml/etree.o -w
  In file included from src/lxml/etree.c:687:0:
  src/lxml/includes/etree_defs.h:14:31: fatal error: libxml/xmlversion.h: 沒有此一檔案或目錄
   #include "libxml/xmlversion.h"
                                 ^
  compilation terminated.
  Compile failed: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
  creating tmp
  cc -I/usr/include/libxml2 -c /tmp/xmlXPathInits6b7t0q3.c -o tmp/xmlXPathInits6b7t0q3.o
  /tmp/xmlXPathInits6b7t0q3.c:1:26: fatal error: libxml/xpath.h: 沒有此一檔案或目錄
   #include "libxml/xpath.h"
                            ^
  compilation terminated.
  *********************************************************************************
  Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
  *********************************************************************************
  error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1

  ----------------------------------------
  Failed building wheel for lxml
  Running setup.py clean for lxml
Failed to build lxml
Installing collected packages: lxml
  Running setup.py install for lxml ... error
    Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-6h5wkzuy/lxml/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-b8h0w1ek-record/install-record.txt --single-version-externally-managed --compile --user --prefix=:
    Building lxml version 4.3.3.
    Building without Cython.
    ERROR: b'/bin/sh: 1: xslt-config: not found\n'
    ** make sure the development packages of libxml2 and libxslt are installed **

    Using build configuration of libxslt
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-armv7l-3.5
    creating build/lib.linux-armv7l-3.5/lxml
  ....... (略) .......
    running build_ext
    building 'lxml.etree' extension
    creating build/temp.linux-armv7l-3.5
    creating build/temp.linux-armv7l-3.5/src
    creating build/temp.linux-armv7l-3.5/src/lxml
    arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fdebug-prefix-map=/build/python3.5-6waWnr/python3.5-3.5.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -DCYTHON_CLINE_IN_TRACEBACK=0 -Isrc -Isrc/lxml/includes -I/usr/include/python3.5m -c src/lxml/etree.c -o build/temp.linux-armv7l-3.5/src/lxml/etree.o -w
    In file included from src/lxml/etree.c:687:0:
    src/lxml/includes/etree_defs.h:14:31: fatal error: libxml/xmlversion.h: 沒有此一檔案或目錄
     #include "libxml/xmlversion.h"
                                   ^
    compilation terminated.
    Compile failed: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
    cc -I/usr/include/libxml2 -c /tmp/xmlXPathInit6reid1bf.c -o tmp/xmlXPathInit6reid1bf.o
    /tmp/xmlXPathInit6reid1bf.c:1:26: fatal error: libxml/xpath.h: 沒有此一檔案或目錄
     #include "libxml/xpath.h"
                              ^
    compilation terminated.
    *********************************************************************************
    Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
    *********************************************************************************
    error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-6h5wkzuy/lxml/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-b8h0w1ek-record/install-record.txt --single-version-externally-managed --compile --user --prefix=" failed with error code 1 in /tmp/pip-build-6h5wkzuy/lxml/

似乎是編譯時出錯, 但密密麻麻不知從何查起. 搜尋 "raspberry pi 3 lxml" 找到下面這篇 : 


從回應中得知安裝 lxml 最簡單的方法是用下面的指令 :


3. 用 apt-get install 安裝 python3-lxml (成功) :

sudo apt-get install python3-lxml python-lxml  (其實只要 python3-lxml 即可)

pi@raspberrypi:~ $ sudo apt-get install python3-lxml python-lxml 
正在讀取套件清單... 完成
正在重建相依關係
正在讀取狀態資料... 完成
下列的額外套件將被安裝:
  python-bs4 python-html5lib python-webencodings python3-bs4 python3-html5lib
  python3-webencodings
建議套件:
  python-genshi python-lxml-dbg python-lxml-doc python3-genshi
  python3-lxml-dbg
下列【新】套件將會被安裝:
  python-bs4 python-html5lib python-lxml python-webencodings python3-bs4
  python3-html5lib python3-lxml python3-webencodings
升級 0 個,新安裝 8 個,移除 0 個,有 0 個未被升級。
需要下載 1,954 kB 的套件檔。
此操作完成之後,會多佔用 8,187 kB 的磁碟空間。
是否繼續進行 [Y/n]? [Y/n] Y
下載:1 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf python-bs4 all 4.5.3-1 [86.7 kB]
下載:2 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf python-webencodings all 0.5-2 [10.3 kB]
下載:3 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf python-html5lib all 0.999999999-1 [88.0 kB]
下載:4 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf python-lxml armhf 3.7.1-1 [793 kB]
下載:5 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf python3-bs4 all 4.5.3-1 [86.6 kB]
下載:6 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf python3-webencodings all 0.5-2 [10.4 kB]
下載:7 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf python3-html5lib all 0.999999999-1 [86.3 kB]
下載:8 http://mirror.ossplanet.net/raspbian/raspbian stretch/main armhf python3-lxml armhf 3.7.1-1 [792 kB]
取得 1,954 kB 用了 9s (200 kB/s)
選取了原先未選的套件 python-bs4。
(讀取資料庫 ... 目前共安裝了 133437 個檔案和目錄。)
Preparing to unpack .../0-python-bs4_4.5.3-1_all.deb ...
Unpacking python-bs4 (4.5.3-1) ...
選取了原先未選的套件 python-webencodings。
Preparing to unpack .../1-python-webencodings_0.5-2_all.deb ...
Unpacking python-webencodings (0.5-2) ...
選取了原先未選的套件 python-html5lib。
Preparing to unpack .../2-python-html5lib_0.999999999-1_all.deb ...
Unpacking python-html5lib (0.999999999-1) ...
選取了原先未選的套件 python-lxml。
Preparing to unpack .../3-python-lxml_3.7.1-1_armhf.deb ...
Unpacking python-lxml (3.7.1-1) ...
選取了原先未選的套件 python3-bs4。
Preparing to unpack .../4-python3-bs4_4.5.3-1_all.deb ...
Unpacking python3-bs4 (4.5.3-1) ...
選取了原先未選的套件 python3-webencodings。
Preparing to unpack .../5-python3-webencodings_0.5-2_all.deb ...
Unpacking python3-webencodings (0.5-2) ...
選取了原先未選的套件 python3-html5lib。
Preparing to unpack .../6-python3-html5lib_0.999999999-1_all.deb ...
Unpacking python3-html5lib (0.999999999-1) ...
選取了原先未選的套件 python3-lxml。
Preparing to unpack .../7-python3-lxml_3.7.1-1_armhf.deb ...
Unpacking python3-lxml (3.7.1-1) ...
設定 python3-webencodings (0.5-2) ...
設定 python3-lxml (3.7.1-1) ...
設定 python3-bs4 (4.5.3-1) ...
設定 python-bs4 (4.5.3-1) ...
設定 python3-html5lib (0.999999999-1) ...
設定 python-lxml (3.7.1-1) ...
設定 python-webencodings (0.5-2) ...
設定 python-html5lib (0.999999999-1) ...

可見連 bs4, htmlib 等套件都順便安裝了. 


5. 匯入 twstock 擷取資料 :

經過以上處理 twstock 才算完整安裝完畢, 即可順利匯入套件進行資料擷取了, 以 2019/04/08 盤後為例 :

pi@raspberrypi:~ $ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
&glt;&glt;&glt; from twstock import Stock   
&glt;&glt;&glt; tw2330=Stock("2330")     #擷取台積電 2330 近 31 日資料
&glt;&glt;&glt; tw2330.price           #近 31 日收盤價
[229.0, 234.5, 236.5, 236.5, 238.0, 239.5, 239.0, 235.5, 233.0, 234.0, 234.0, 230.0, 230.5, 235.5, 237.0, 234.5, 239.0, 241.0, 240.5, 242.0, 245.5, 248.5, 241.5, 244.0, 241.5, 242.0, 245.5, 245.5, 246.0, 246.5, 253.0]
&glt;&glt;&glt; tw2330.price[0]       #近 1 日收盤價
229.0
&glt;&glt;&glt; tw2330.price[30]     #31 前收盤價
253.0
&glt;&glt;&glt; tw2330.price[-5:]     #近 5 日收盤價
[245.5, 245.5, 246.0, 246.5, 253.0]
&glt;&glt;&glt; tw2330.close             #近 31 日收盤價
[229.0, 234.5, 236.5, 236.5, 238.0, 239.5, 239.0, 235.5, 233.0, 234.0, 234.0, 230.0, 230.5, 235.5, 237.0, 234.5, 239.0, 241.0, 240.5, 242.0, 245.5, 248.5, 241.5, 244.0, 241.5, 242.0, 245.5, 245.5, 246.0, 246.5, 253.0]
&glt;&glt;&glt; tw2330.change         #近 31 日漲跌幅 (元)
[-1.0, 5.5, 2.0, 0.0, 1.5, 1.5, -0.5, -3.5, -2.5, 1.0, 0.0, -4.0, 0.5, 5.0, 1.5, -2.5, 4.5, 2.0, -0.5, 1.5, 3.5, 3.0, -7.0, 2.5, -2.5, 0.5, 3.5, 0.0, 0.5, 0.5, 6.5]
&glt;&glt;&glt; tw2330.open             #近 31 日開盤價
[230.0, 231.5, 235.5, 235.5, 237.5, 239.0, 238.5, 239.5, 234.0, 235.0, 235.0, 231.0, 228.0, 233.0, 234.0, 239.0, 237.0, 239.5, 239.0, 242.0, 242.5, 248.0, 240.5, 243.0, 242.0, 240.5, 243.0, 251.0, 249.5, 249.0, 251.0]
&glt;&glt;&glt; tw2330.low               #近 31 日最低價
[229.0, 231.0, 234.5, 234.0, 236.0, 236.5, 237.0, 234.5, 232.5, 233.5, 233.0, 229.0, 227.5, 232.5, 234.0, 234.0, 235.5, 239.0, 238.0, 239.5, 241.0, 244.5, 240.5, 241.0, 240.5, 240.0, 240.5, 245.0, 246.0, 246.5, 250.5]
&glt;&glt;&glt; tw2330.high              #近 31 日最高價
[230.5, 234.5, 236.5, 236.5, 239.0, 240.0, 239.5, 239.5, 234.5, 235.5, 235.0, 231.5, 230.5, 237.0, 237.0, 239.0, 239.5, 241.0, 240.5, 242.0, 245.5, 248.5, 243.5, 244.0, 242.5, 242.5, 245.5, 251.0, 249.5, 249.0, 253.0]
&glt;&glt;&glt; tw2330.capacity        #近 31 日成交股數
[18423023, 31887710, 34013489, 22032163, 25227138, 24586789, 37947889, 45164513, 23708407, 20811226, 17226192, 22535659, 34282793, 23839972, 22493195, 17281356, 34542729, 27040532, 14620009, 23055971, 27514502, 22996467, 19273254, 21053170, 23622089, 13998054, 31024810, 35330656, 25189053, 28581323, 46426821]
&glt;&glt;&glt; tw2330.turnover       #近 31 日成交金額
[4228108427, 7430197670, 8013908284, 5187054985, 5995746054, 5864298571, 9052540803, 10691130829, 5533050748, 4872352124, 4029782428, 5183171608, 7835079913, 5601351324, 5308051755, 4077045380, 8231572752, 6493129050, 3496720720, 5563557011, 6710510004, 5680726299, 4662705968, 5109273236, 5703467700, 3379510718, 7568601014, 8758161220, 6226523038, 7075597562, 11682512762]
&glt;&glt;&glt; tw2330.transaction      #近 31 日成交筆數
[6471, 14241, 11443, 6913, 8527, 7994, 9931, 13323, 8706, 7090, 5768, 8931, 7451, 9253, 6269, 6564, 8872, 10690, 7105, 6757, 11071, 10485, 8468, 6630, 7603, 3948, 6712, 13292, 9171, 8067, 16430]
&glt;&glt;&glt; tw2330.raw_data          #近 31 日原始資料
[{'stat': 'OK', 'title': '108年02月 2330 台積電           各日成交資訊', 'fields': ['日期', '成交股數', '成交金額', '開盤價', '最高價', '最低價', '收盤價', '漲跌價差', '成交筆數'], 'notes': ['符號說明:+/-/X表示漲/跌/不比價', '當日統計資訊含一般、零股、盤後定價、鉅額交易,不含拍賣、標購。', 'ETF證券代號第六碼為K、M、S、C者,表示該ETF以外幣交易。'], 'date': '20190201', 'data': [Data(date=datetime.datetime(2019, 2, 11, 0, 0), capacity=81360106, turnover=18546167132, open=228.0, high=229.0, low=226.5, close=228.0, change=7.0, transaction=21689), Data(date=datetime.datetime(2019, 2, 12, 0, 0), capacity=30125670, turnover=6921488930, open=230.0, high=230.0, low=229.0, close=230.0, change=2.0, transaction=13313), Data(date=datetime.datetime(2019, 2, 13, 0, 0), capacity=28634925, turnover=6575201825, open=232.0, high=232.0, low=228.5, close=229.0, change=-1.0, transaction=10230), Data(date=datetime.datetime(2019, 2, 14, 0, 0), capacity=23647751, turnover=5392943977, open=229.5, high=230.0, low=226.0, close=227.0, change=-2.0, transaction=6873), Data(date=datetime.datetime(2019, 2, 15, 0, 0), capacity=22369799, turnover=5093773373, open=229.0, high=229.0, low=226.5, close=227.0, change=0.0, transaction=5743), Data(date=datetime.datetime(2019, 2, 18, 0, 0), capacity=18163916, turnover=4169729625, open=229.0, high=230.5, low=228.5, close=230.0, change=3.0, transaction=7035), Data(date=datetime.datetime(2019, 2, 19, 0, 0), capacity=18423023, turnover=4228108427, open=230.0, high=230.5, low=229.0, close=229.0, change=-1.0, transaction=6471), Data(date=datetime.datetime(2019, 2, 20, 0, 0), capacity=31887710, turnover=7430197670, open=231.5, high=234.5, low=231.0, close=234.5, change=5.5, transaction=14241), Data(date=datetime.datetime(2019, 2, 21, 0, 0), capacity=34013489, turnover=8013908284, open=235.5, high=236.5, low=234.5, close=236.5, change=2.0, transaction=11443), Data(date=datetime.datetime(2019, 2, 22, 0, 0), capacity=22032163, turnover=5187054985, open=235.5, high=236.5, low=234.0, close=236.5, change=0.0, transaction=6913), Data(date=datetime.datetime(2019, 2, 25, 0, 0), capacity=25227138, turnover=5995746054, open=237.5, high=239.0, low=236.0, close=238.0, change=1.5, transaction=8527), Data(date=datetime.datetime(2019, 2, 26, 0, 0), capacity=24586789, turnover=5864298571, open=239.0, high=240.0, low=236.5, close=239.5, change=1.5, transaction=7994), Data(date=datetime.datetime(2019, 2, 27, 0, 0), capacity=37947889, turnover=9052540803, open=238.5, high=239.5, low=237.0, close=239.0, change=-0.5, transaction=9931)]}, {'stat': 'OK', 'title': '108年03月 2330 台積電           各日成交資訊', 'fields': ['日期', '成交股數', '成交金額', '開盤價', '最高價', '最低價', '收盤價', '漲跌價差', '成交筆數'], 'notes': ['符號說明:+/-/X表示漲/跌/不比價', '當日統計資訊含一般、零股、盤後定價、鉅額交易,不含拍賣、標購。', 'ETF證券代號第六碼為K、M、S、C者,表示該ETF以外幣交易。'], 'date': '20190301', 'data': [Data(date=datetime.datetime(2019, 3, 4, 0, 0), capacity=45164513, turnover=10691130829, open=239.5, high=239.5, low=234.5, close=235.5, change=-3.5, transaction=13323), Data(date=datetime.datetime(2019, 3, 5, 0, 0), capacity=23708407, turnover=5533050748, open=234.0, high=234.5, low=232.5, close=233.0, change=-2.5, transaction=8706), Data(date=datetime.datetime(2019, 3, 6, 0, 0), capacity=20811226, turnover=4872352124, open=235.0, high=235.5, low=233.5, close=234.0, change=1.0, transaction=7090), Data(date=datetime.datetime(2019, 3, 7, 0, 0), capacity=17226192, turnover=4029782428, open=235.0, high=235.0, low=233.0, close=234.0, change=0.0, transaction=5768), Data(date=datetime.datetime(2019, 3, 8, 0, 0), capacity=22535659, turnover=5183171608, open=231.0, high=231.5, low=229.0, close=230.0, change=-4.0, transaction=8931), Data(date=datetime.datetime(2019, 3, 11, 0, 0), capacity=34282793, turnover=7835079913, open=228.0, high=230.5, low=227.5, close=230.5, change=0.5, transaction=7451), Data(date=datetime.datetime(2019, 3, 12, 0, 0), capacity=23839972, turnover=5601351324, open=233.0, high=237.0, low=232.5, close=235.5, change=5.0, transaction=9253), Data(date=datetime.datetime(2019, 3, 13, 0, 0), capacity=22493195, turnover=5308051755, open=234.0, high=237.0, low=234.0, close=237.0, change=1.5, transaction=6269), Data(date=datetime.datetime(2019, 3, 14, 0, 0), capacity=17281356, turnover=4077045380, open=239.0, high=239.0, low=234.0, close=234.5, change=-2.5, transaction=6564), Data(date=datetime.datetime(2019, 3, 15, 0, 0), capacity=34542729, turnover=8231572752, open=237.0, high=239.5, low=235.5, close=239.0, change=4.5, transaction=8872), Data(date=datetime.datetime(2019, 3, 18, 0, 0), capacity=27040532, turnover=6493129050, open=239.5, high=241.0, low=239.0, close=241.0, change=2.0, transaction=10690), Data(date=datetime.datetime(2019, 3, 19, 0, 0), capacity=14620009, turnover=3496720720, open=239.0, high=240.5, low=238.0, close=240.5, change=-0.5, transaction=7105), Data(date=datetime.datetime(2019, 3, 20, 0, 0), capacity=23055971, turnover=5563557011, open=242.0, high=242.0, low=239.5, close=242.0, change=1.5, transaction=6757), Data(date=datetime.datetime(2019, 3, 21, 0, 0), capacity=27514502, turnover=6710510004, open=242.5, high=245.5, low=241.0, close=245.5, change=3.5, transaction=11071), Data(date=datetime.datetime(2019, 3, 22, 0, 0), capacity=22996467, turnover=5680726299, open=248.0, high=248.5, low=244.5, close=248.5, change=3.0, transaction=10485), Data(date=datetime.datetime(2019, 3, 25, 0, 0), capacity=19273254, turnover=4662705968, open=240.5, high=243.5, low=240.5, close=241.5, change=-7.0, transaction=8468), Data(date=datetime.datetime(2019, 3, 26, 0, 0), capacity=21053170, turnover=5109273236, open=243.0, high=244.0, low=241.0, close=244.0, change=2.5, transaction=6630), Data(date=datetime.datetime(2019, 3, 27, 0, 0), capacity=23622089, turnover=5703467700, open=242.0, high=242.5, low=240.5, close=241.5, change=-2.5, transaction=7603), Data(date=datetime.datetime(2019, 3, 28, 0, 0), capacity=13998054, turnover=3379510718, open=240.5, high=242.5, low=240.0, close=242.0, change=0.5, transaction=3948), Data(date=datetime.datetime(2019, 3, 29, 0, 0), capacity=31024810, turnover=7568601014, open=243.0, high=245.5, low=240.5, close=245.5, change=3.5, transaction=6712)]}, {'stat': 'OK', 'title': '108年04月 2330 台積電           各日成交資訊', 'fields': ['日期', '成交股數', '成交金額', '開盤價', '最高價', '最低價', '收盤價', '漲跌價差', '成交筆數'], 'notes': ['符號說明:+/-/X表示漲/跌/不比價', '當日統計資訊含一般、零股、盤後定價、鉅額交易,不含拍賣、標購。', 'ETF證券代號第六碼為K、M、S、C者,表示該ETF以外幣交易。'], 'date': '20190401', 'data': [Data(date=datetime.datetime(2019, 4, 1, 0, 0), capacity=35330656, turnover=8758161220, open=251.0, high=251.0, low=245.0, close=245.5, change=0.0, transaction=13292), Data(date=datetime.datetime(2019, 4, 2, 0, 0), capacity=25189053, turnover=6226523038, open=249.5, high=249.5, low=246.0, close=246.0, change=0.5, transaction=9171), Data(date=datetime.datetime(2019, 4, 3, 0, 0), capacity=28581323, turnover=7075597562, open=249.0, high=249.0, low=246.5, close=246.5, change=0.5, transaction=8067), Data(date=datetime.datetime(2019, 4, 8, 0, 0), capacity=46426821, turnover=11682512762, open=251.0, high=253.0, low=250.5, close=253.0, change=6.5, transaction=16430)]}]




Bingo! 大功告成!  不過 Yahoo 顯示成交張數是 45133, 但 twstock 抓到的是 46426821 股, 換算是 46426 張, 多了 1293 張, 這是何故?

總結以上安裝經驗, 應該是先安裝相依套件 analytics 與 lxml, 然後再安裝 twstock, 指令如下 : 

pip3 install analytics
sudo apt-get install python3-lxml python-lxml 
pip3 install twstock 

沒有留言:

張貼留言