ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[­ì³Ð] python¤W¥«Âd¤T¤jªk¤H¶R½æ¶W¤é³ø¸ê®Æ¤U¸ü

[­ì³Ð] python¤W¥«Âd¤T¤jªk¤H¶R½æ¶W¤é³ø¸ê®Æ¤U¸ü

¥i¥Î¨Ó¤U¸ü¤W¥«Âd¤T¤jªk¤H¶R½æ¶W¤é³ø¸ê®Æ¡C»PpyÀɦP¤@¸ô®|¤Uªºpath.ini¥i³]©wcsv¦s©ñ¦ì¸m¡Aª`·N¸ô®|­n¥]¬A³Ì«á¤@­Ó"\"¡A¦p¡GC:\user\desktop\test\¡C
ªþ¤W°õ¦æÀÉ
  1. import requests
  2. import time
  3. import os

  4. mydate = time.strftime("%Y/%m/%d")
  5. year = str(int(mydate[0:4])-1911)
  6. mydate2 = mydate.replace(mydate[0:4],year)

  7. setting = os.getcwd() + "\\" + "path.ini"

  8. if os.path.exists(setting):
  9.     with open(setting, 'r') as r:
  10.         path = r.read()
  11.     if not os.path.exists(path):
  12.         os.makedirs(path)
  13. else:
  14.     path = ""

  15. headers = {"User-Agent":"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"}


  16. url1 = "http://www.twse.com.tw/ch/trading/fund/T86/T86.php"
  17. payload1 = {"download": "csv",
  18.             "qdate": mydate2,
  19.             "select2": "ALLBUT0999",
  20.             "sorting": "by_issue"}

  21. res1 = requests.post(url1, headers=headers, data=payload1, stream=True)

  22. fName1 = path + mydate.replace("/","") + ".csv"

  23. with open(fName1, 'wb') as f1:
  24.     for chunk in res1.iter_content(1024):
  25.         f1.write(chunk)


  26. url3 = "http://www.tpex.org.tw/web/stock/3insti/daily_trade/\
  27. 3itrade_hedge_download.php?l=zh-tw&se=EW&t=D&d=" + mydate2 + "&s=0,asc,0"

  28. res3 = requests.get(url3, headers=headers, stream=True)

  29. fName3 = path + "BIGD_" + mydate2.replace("/","") + ".csv"

  30. with open(fName3, 'wb') as f3:
  31.     for chunk in res3.iter_content(1024):
  32.         f3.write(chunk)


  33. url2 = "http://www.twse.com.tw/ch/trading/exchange/MI_INDEX/MI_INDEX.php"
  34. payload2 = {"download": "csv",
  35.             "qdate": mydate2,
  36.             "selectType": "ALLBUT0999"}

  37. res2 = requests.post(url2, headers=headers, data=payload2,stream=True)

  38. fName2= path + "A11" + mydate.replace("/","") + ".csv"

  39. with open(fName2, 'wb') as f2:
  40.     for chunk in res2.iter_content(1024):
  41.         f2.write(chunk)
  42.         

  43. url4 = "http://www.tpex.org.tw/web/stock/aftertrading/otc_quotes_no1430/\
  44. stk_wn1430_download.php?l=zh-tw&d=" + mydate2 + "&se=EW&s=0,asc,0"

  45. res4 = requests.post(url4, headers=headers, data=payload2,stream=True)

  46. fName4 = path + "SQUOTE_EW_" + mydate2.replace("/","") + ".csv"

  47. with open(fName4, 'wb') as f4:
  48.     for chunk in res4.iter_content(1024):
  49.         f4.write(chunk)
½Æ»s¥N½X

¥»©«³Ì«á¥Ñ VBALearner ©ó 2016-8-16 23:10 ½s¿è

¦^´_ 1# zyzzyva

µ¥¤@¤U!!!°®~~~³o¤Ó²r¤F§aO___Q ©~µM¹L¤F¤@¬q®É¶¡...ÁÙ¦³¤H¦A¬ã¨s§Úªº¥DÃD¡A¦³Ãö¦p¦ó¤U¸üªº¤º®e......¥u¯à»¡·P°ÊT___T
¤p§Ì§Ú­ì¥»·Q¦A¥á¤@­Ó¶}µo¨ì¤@¥bªºÄw½X¸ê®Æ¾ã²zÀÉ¡A¤ºªþ¿ïªÑªí³æ¥\¯à»P¶i³õÁZ®Ä¬ö¿ý¥\¯à¡A©MZ¤j¦@Á¸²±Á|­«±Ò³o­Ó¥DÃD¨Ã¤À¨É¨ä©Ò¾Ç¡A¦ýê©ó¤W¶Ç­­¨î(1MB)¡A¥u¦n§@½}¡A¥H¤å¦r¥æ¬y¤§(¹Ä~)
Z¤j°ª¤â!!! ¤p§Ì§Ú³Ìªñ­è¦n¤]¶}©l·Q¾Ç³Ìªñ«Ü¤õ¬õªºPython¡A¥u¤£¹LÁÙ¦bºN¯Á¶¥¬q¡A²¦³º¥«­±¤W¦³ÃöPyªº®Ñ¤£¦h¡A¤£¹³VBA³oºØºâ¬O¼öªù¤S²³æªº®ÑÄy...Z¤j¯u¬O¤Ä°_§Ú¹ïPyhtonµ{¦¡ªº²ö¤j¿³½ì>w<!!! ¥i¥H½Ð±Ð±z¡A´X­Ó°ÝÃD¶Ü? ²Ä¤@¡A³o­Ó°õ¦æÀɬO¥þ³¡³£¥ÎPython¼gªº¶Ü?!! ±z¶K¤W¨Óªºµ{¦¡½X¬O¥ÎPython¼gªº¶Ü? ÁÙ¬OPy»PVBA¨âªÌµ²¦X©O?!(¬Ý°_¨Ó¤£¹³¦³°ÑÂøVBA»yªk...§Ú´X¥G¬Ý¤£À´xD¡A±z©ó¤åªìÁöµM¦³Â²³æ»¡©ú¡A¦ý¤p§Ì§ÚÁÙ¬O¬ÇµZ¿i¤F¡A¥ý¤@®Çª±¨F¥h~) ¡C²Ä¤G¡A±z¬O¦p¦ó¾Ç²ßPyªºxD? ¬O¦Û¾Ç¡B¤W½Ò¡BÁÙ¬O¦³Ãþ¦üªº½×¾Â©O?  ´Á±æ¯à¯¸¦bZ¤jªºªÓ»H¤W¾Ç²ßÁÚ¶iOwO!!!
¯u¤ß·PÁ¨C¤@¦ìÄ@·N¤À¨É©Ò¾Ç¡B«ü¾É·s¤âªº¤H!

TOP

¦^´_ 2# VBALearner
±z¹L¼ú¤F¡C³o­Ó¬Opythonªºcode¡A¦pªG¨S¦³¦w¸Ëpython¡A¥i¥H¤U¸ü°õ¦æÀÉ¡A¦A¦bvba¸Ì¥Îshellªº¤è¦¡°õ¦æ¡C
§ìºô¸ô¸ê®Æ¤è­±pythonÁÙº¡¦n¥Îªº¡Aºô¸ô¤W¸ê·½«Ü¦h¡A§Ú¤]¬O¬°¤F§ì¸ê®ÆªF¾Ç¤@ÂI¦è¾Ç¤@ÂI¡A®³§O¤HªºªF¦è­×­×§ï§ï¡C§ä®É¶¡¦A¸É¤Wµù¸Ñ¡AÀ³¸Ó·|¤ñ¸û²M·¡¡C

TOP

ª©¤j
±zªºÀɮפU¸ü«á¡A°õ¦ægetTwseOtc.exe«á¨S¦³¤ÏÀ³¡A¥i¥H¦A»¡©ú²M·¡¤@ÂI¶Ü?
ÁÂÁÂ

TOP

¦^´_ 3# zyzzyva
Àˬd¥Ø¿ý¤º¤§Àɮפº®e¦p¹Ï¥Ü¡G

TOP

¦^´_ 5# c_c_lai

¦A­«·s¤U¸ü¸ÑÀ£ÁY«á¥i¥¿±`°õ¦æ
TKS
¬ã¨sPYTHON¤¤~~

TOP

¦^´_ 5# c_c_lai

À³¸Ó¬O¦]¬°§Ú¨S¦³³B²z®É¶¡(¤U¸üªº¤é´Á¬Oª½±µ¨ú·í¤éªº¤é´Á)¡A¦pªG¬O¸ê®ÆÁÙ¨S¥X¨Óªº®É­Ô°õ¦æ¤U¸ü´N·|¥X²{°ÝÃD¡C
½Ð¦b³Ä±ßªº®É¶¡¦A¸Õ¸Õ¬Ý¡C

TOP

¦^´_ 7# zyzzyva
­è­è§Ú¸Õ¤F¤@¤U¡AOK ¤F¡C
½Ð±Ð¤@¤U§Ú¦³¤U¸ü Python35 ¦ý¬OÁٻݭn«ç¼Ëªº¼Ò²Õ
±ø¥ó¤U¡A¤~¯à§¹¾ã°õ¦æ§Aªþ¤Wªº .py ÀÉ©O¡H
Python ¬O¦³½m²ß¹L¡A¦ýµL¹ê°È§@·~¸gÅç¡A
ÁٽЦh¦h«ü±Ð¡I

TOP

¦^´_ 1# zyzzyva
¸É¤Wµù¸Ñ¡C
  1. #¶×¤J¼Ò²Õ(requests»Ý¥t¥~¦w¸Ë¡A¤]¥i¨Ï¥Îurllib2)
  2. import requests
  3. import time
  4. import os

  5. #¨ú±o·í«e¤é´Á
  6. mydate = time.strftime("%Y/%m/%d")

  7. #±N¦è¤¸¦~Âର¥Á°ê¦~
  8. year = str(int(mydate[0:4])-1911)

  9. #±N¥Á°ê¦~¨ú¥N¤é´Á¤¤¤§¦è¤¸¦~
  10. mydate2 = mydate.replace(mydate[0:4],year)

  11. #os.getcwd()¨ú±o·í«e¸ô®|
  12. setting = os.getcwd() + "\\" + "path.ini"

  13. #Àˬdpath.ini¬O§_¦s¦b¡A­Y¦³«h±Ncsv¦sÀɤ§¸ô®|Ū¤J
  14. if os.path.exists(setting):
  15.     with open(setting, 'r') as r:
  16.         path = r.read()
  17.     #­Ycsv¦sÀɤ§¸ê®Æ§¨¤£¦s¦b«h«Ø¥ß¸ê®Æ§¨
  18.     if not os.path.exists(path):
  19.         os.makedirs(path)

  20. #­Ypath.ini¤£¦s¦b«h±Npath³]¬°ªÅ¦r¦ê
  21. else:
  22.     path = ""

  23. #³]©wheader
  24. headers = {"User-Agent":"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"}

  25. #³]©wurl
  26. url1 = "http://www.twse.com.tw/ch/trading/fund/T86/T86.php"

  27. #³]©wpost¬d¸ß¤§¸ê®Æ
  28. payload1 = {"download": "csv",
  29.             "qdate": mydate2,
  30.             "select2": "ALLBUT0999",
  31.             "sorting": "by_issue"}

  32. #µo¥Xrequest¨Ã±Nºô¯¸¦^ÂФ§¸ê®Æ¦s¤Jres1ÅܼƤ¤
  33. res1 = requests.post(url1, headers=headers, data=payload1, stream=True)

  34. #³]©wcsvÀɮפ§¸ô®|»PÀɦW
  35. fName1 = path + mydate.replace("/","") + ".csv"

  36. #±Nres1ªº¸ê®Æ¼g¤Jcsv¤¤
  37. with open(fName1, 'wb') as f1:
  38.     for chunk in res1.iter_content(1024):
  39.         f1.write(chunk)
½Æ»s¥N½X

TOP

¦^´_ 8# c_c_lai
¥u­n¦w¸Ërequests´N¥i¥H¤F¡C

TOP

        ÀR«ä¦Û¦b : ¡i¬O§_µo´§¤F¨}¯à¡H¡j¤H¶¡¹Ø©R¦]¬°µu¼È¡A¤~§óÅã±o¬Ã¶Q¡CÃø±o¨Ó¤@½ë¤H¶¡¡AÀ³°Ý¬O§_¬°¤H¶¡µo´§¤F¦Û¤vªº¨}¯à¡A¦Ó¤£­n¤@¨ý¨Dªø¹Ø¡C
ªð¦^¦Cªí ¤W¤@¥DÃD