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

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

¦^´_ 103# koshi0413
ÁÂÁ§A¡I
§Ú¦n¦n¦a¨Ó¬ã¨s¤@¤U¡CBS(res.text) §Ú­×§ï¦¨ BS(res.text, 'lxml')
³Ì«á¤§ print( ... ) (¦]§Úªºª©¥»¬O 3.5) §Ú¦A·Q·Q¬Ý¦³¨S¦³§ó¨Îªºªí¹F³B²z¡C
½Ð±Ð soup.select('table')[1] ¬°¦ó¬O [1]¡H soup.select('tr')[3:] ªº [3:] «üªº¤S¬O¡H
Python §Úºâ¬O¥®¨à¥Í¡AÁÙ±o¦V§A­Ì½Ð¯q¤F¡C ÁÂÁÂÅo¡I

TOP

¥»©«³Ì«á¥Ñ koshi0413 ©ó 2016-9-12 20:33 ½s¿è

¤p§Ì§Ú²×©ó¥i¥H¦^ÂФF¡A·P°Ê!!
³o¤å³¹§Ú°l«Ü¤[¤F python ¤]¬O¬Ý¨ì³o½g¤~¶}©l¾Çªº¡A
¦b¦¹½×¾Â¾Ç¤F¤£¤Övba  ¤]¬Ý¤F¨Çc_c_lai ¾Ç¨ì¤£¤Ö¡A­è¦n¬Ý¨ìc_c_lai¦³´£°Ý¡A¨Ó³øµª¤@¤U
¤U­±¬° c_c_lai  ¤§»Ý¨D¡A¤U¸ücsv«Ü¤è«K¡A¤]«Ü§Ö!!!  ¦ý¶×¤J SQL³Â·Ð¡A­n¦bÂà½X¡A¬G¤p§Ì·|¿ï¾Üª½±µ´£¨ú¡]¨ä¹ê¬OÁÙ¤£·|¥Îpythonª½±µÂàcsv½X¦b¦Û°Ê¶×¤JSQL¡^
ps:¥N½X¬°zyzzyva­×§ïª©
pps:·½½X¤¤¬q¸¨¦³ # ¬Ò¬°´£¨úºô­¶®É¡A¤ÏÂйêÅç¥Î¡A³oºô­¶¬°¤F´£¨ú©Ò¦Ó¬q¸¨¤]ªá¤F¤@¤p®É¨Ó¸Õ¦UºØºô­¶¥N½X¨Ó¥ÎBeautifulSoup´£¨ú
  1. # -*- coding: utf-8 -*-
  2. import requests
  3. import time
  4. import os
  5. from bs4 import BeautifulSoup as BS
  6. from datetime import date

  7. headers = {"User-Agent":"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"}
  8. url1 = "µ¥¯Å¤£°÷¡Aºô§}½Ð¦Û¦æ¿é¤J"
  9. payload = {"download":'',
  10.             "qdate":'105/09/10',
  11.             "selectType":"ALL"}
  12. res = requests.post(url1, headers=headers, data=payload)
  13. #print res.text

  14. soup = BS(res.text)
  15. #print soup.select('.board_trad')[0].text
  16. #tb = soup.select('#main-content')
  17. #print tb
  18. #tb = soup.findAll('table')
  19. soup = soup.select('table')[1]
  20. #print tb
  21. for ta in soup.select('tr')[3:]:
  22.     print ta.select("td")[0].text,ta.select("td")[1].text,ta.select("td")[2].text,ta.select("td")[3].text,ta.select("td")[4].text,ta.select("td")[5].text,ta.select("td")[6].text,ta.select("td")[7].text,ta.select("td")[8].text,ta.select("td")[9].text,ta.select("td")[10].text,ta.select("td")[11].text,ta.select("td")[12].text,ta.select("td")[13].text,ta.select("td")[14].text
½Æ»s¥N½X
³o¬Oµ²ªG¡C¥upo«e´X¬q
0050   ¤¸¤j¥xÆW50       194 104 0 1,056 1,146 194,125 0 5 0 496 501 194,125 19
0051   ¤¸¤j¤¤«¬100      0 0 0 2 2 4,500 0 0 0 0 0 4,500 0
0052   FB¬ì§Þ           0 0 0 0 0 2,000 0 0 0 0 0 2,000 0
0053   ¤¸¤j¹q¤l         0 0 0 0 0 3,247 0 0 0 0 0 3,247 0
0054   ¤¸¤j¥x°Ó50       0 0 0 0 0 4,656 0 0 0 0 0 4,656 0
0055   ¤¸¤jMSCIª÷¿Ä     20 5 0 622 637 17,163 0 4 0 118 122 17,163 0
0056   ¤¸¤j°ªªÑ®§       7 1 0 94 100 67,508 4 0 0 25 21 67,508 3
0057   FB¼¯¥x           0 0 0 0 0 2,006 0 0 0 0 0 2,006 0
0058   FBµo¹F           0 0 0 0 0 1,299 0 0 0 0 0 1,299 0

TOP

¦^´_ 100# zyzzyva
°²³]¤£¦s¦¨ csv¡A¦Ó¤@¤@Åã¥Ü¤S·|¬O¦p¦ó³B­ù¡A
ª½ºI¨Ï¥Î res¡A¦n¹³µL°Ê§@¡C(·U¨Ó·U³ß·R Python ¤F)
ÁÂÁÂÅo¡I

TOP

¦^´_ 100# zyzzyva
Ãø©Ç¤j®a·|¨Ó¨Ï¥Î Python¡A¥ú¬O¬Ý¨ä°õ¦æ¯à¤O´N¨¬¨o¡C
­ì¨Ó¬O¨º»ò¦a²¼ä¡B§ã­n¡A¤S¾Ç¨ì¤F¡AÁÂÁ§Aªº¤£§[«ü¾É¡I

TOP

¦^´_ 99# c_c_lai
³o­Ó¦³´£¨Ñcsv¤U¸üªü¡A¥Î§Ú­Ì³o­Ó°Q½×¦ê¶}ÀYªº¤è¦¡ª½±µ§ìcsvÀ³¸Ó¬O³Ì§Öªº¡C
  1. import requests

  2. headers = {"User-Agent":"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"}
  3.            
  4. url = 'http://www.twse.com.tw/ch/trading/exchange/MI_MARGN/MI_MARGN.php'

  5. payload={'download':'csv',
  6.         'qdate':'105/09/07',
  7.         'selectType':'ALL'}

  8. res = requests.post(url, headers=headers, data=payload, stream=True)

  9. with open('test.csv', 'wb',) as f:
  10.     for chunk in res.iter_content(1024):
  11.         f.write(chunk)
½Æ»s¥N½X

TOP

¦^´_ 98# zyzzyva
«D±`·PÁ§Aªº¸Ô²Ó»¡©ú¡C
¤âÀY¤W¦³¤@²{¦¨ªº Excel ½d¨Ò¡A§Ú±N¨ä¤é´Á­q¬° 105¦~09¤ë08¤é¡A
À³¥Î IE ª«¥ó¥h§ì¨ú¸Ó¤éªº "«H¥Î¥æ©ö²Î­p"¡C§Ú«D±`§Æ±æ¯àÂǥѦ¹½d¨Ò
¨Ï¥Î Python ¨Ó¹F¦¨¡A¤@¤è­±¼Wªø´¼¼z¡B¥t¤@¤è­±±o¥H¼W¶i Python ªº¾Ç²ß¡A
¤Î¶i¤@¨B¤§¤F¸Ñ»PÀ³¥Î¡C¥ý¦æÁÂÁ§AÅo¡I
  1. Sub «H¥Î¥æ©ö²Î­p()
  2.     Dim i As Integer, j As Integer
  3.     Dim ie As Object, E As Object, Sh As Worksheet
  4.         
  5.     Set ie = CreateObject("InternetExplorer.Application")
  6.    
  7.     With ie
  8.         .Visible = True
  9.         .navigate "http://www.twse.com.tw/ch/trading/exchange/MI_MARGN/MI_MARGN.php"
  10.         
  11.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  12.         
  13.         .document.getElementById("date-field").Value = Format("2016/9/8", "EE/MM/DD")    '  ¶ñ¤J
  14.                  
  15.         .document.all("selectType").SelectedIndex = 1      '   1 ¥þ³¡; 7 ¤ôªd¤u·~; 8 ­¹«~¤u·~
  16.         .document.all("query-button").Click
  17.         
  18.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  19.         
  20.         '  Set Sh = ActiveSheet
  21.         Set Sh = Sheets("«H¥Î¥æ©ö²Î­p")                     '  ¤u§@ªí³æ¦WºÙ
  22.         Sh.UsedRange.Clear
  23.         '  Sh.[A:A].NumberFormatLocal = "G/³q¥Î®æ¦¡"        '  ¸Ñ¨M "0050"  ->  "50" (¤£²z·Q)
  24.         '  Sh.[A:A].NumberFormatLocal = "@"                 '  ¸Ñ¨M "0050"  ->  "50" (¥ª¤W¨¤·|¦³¤T¨¤§Î)
  25.         
  26.         i = 0
  27.         With .document
  28.             For Each E In .all.tags("table")(3).Rows
  29.                 i = i + 1 '
  30.                 For j = 0 To E.Cells.Length - 1
  31.                     If j = 0 And Left(E.Cells(j).innerText, 1) = "0" Then    '  §ï¥H .Formula ªº¤è¦¡³B²z
  32.                         Sh.Cells(i, j + 1).Formula = "=""" & E.Cells(j).innerText & """"
  33.                     Else
  34.                         Sh.Cells(i, j + 1) = E.Cells(j).innerText
  35.                     End If
  36.                 Next
  37.             Next
  38.             
  39.             i = i + 1         '  ¶¡¹j¥X¤@ªÅ¥Õ¦æ¡A©ö©ó¤W¤U°Ï¹j¿ëÃÑ
  40.             For Each E In .all.tags("table")(4).Rows
  41.                 i = i + 1 '
  42.                 For j = 0 To E.Cells.Length - 1
  43.                     If j = 0 And Left(E.Cells(j).innerText, 1) = "0" Then
  44.                         Sh.Cells(i, j + 1).Formula = "=""" & E.Cells(j).innerText & """"
  45.                     Else
  46.                         Sh.Cells(i, j + 1) = E.Cells(j).innerText
  47.                     End If
  48.                 Next
  49.             Next
  50.         End With
  51.         
  52.         .Quit
  53.         With Sh
  54.             .[A:A].HorizontalAlignment = xlLeft           '  A Äæ­È¥þ¼Æ¾a¥ª
  55.             .Select
  56.         End With
  57.     End With
  58. End Sub
½Æ»s¥N½X
«H¥Î¥æ©ö²Î­p.rar (51.88 KB)

TOP

¦^´_ 96# c_c_lai
r'/\s\d{1,3}'¡G¦r¦ê«e­±¥[¤Wr¬Oªí¥Üraw string¡A´N¬O³qª¾python¤£­n²z·|¯S®í¦r¤¸¡A·Ó¦r¦ê­ì¥»ªº¼Ë¤l¥N¶i¥hre module¡C
«á­±´N¬Oreªºªí¥Ü¡A¥H¡uÁ`¦@ 5704 µ§¸ê®Æ ¡m¡m¤W¤@­¶ ­¶¦¸ 1 / 286 ¤U¤@­¶¡n¡n¡v¨Ó»¡¡A ¡u/¡v´N¬O¦b¨â­Ó¼Æ¦r(1¡B286)¤§¶¡
¡u/¡v¤§«á¦³¤@­ÓªÅ¥Õ(¦bre¸Ì´N¬O¡u\s¡v¡A«á­±¡u\d¡vªí¥Ü¼Æ¦r¡A¡u{1,3}¡vªí¥Ü¦³1~3­Ó«e­±ªºªF¦è(¥H³o¸Ì¨Ó»¡´N¬O¡u\d¡v¡C
BeautifulSoup¤£¤@©w­n¥Îlxml°µ¬°parser¡Aºô­¶¦pªGµ²ºc¨}¦n¡A¥Î¨ººØ®t§O¨ä¹ê¤£¤j(lxml³t«×¥i¯à¦n¤@¨Ç)¡C
¥i¥H°Ñ¦Òhttps://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/#id49
soup.select('.tb_pages td')ªð¦^ªº¬O¤@­Ólist¡Alist[0]´N¬Olistªº²Ä¤@­Ó¤¸¯Àªº·N«ä¡C
regular expressionªº³t«×º¡¦nªº¡AÀ³¸Ó¦U»y¨¥³£¦³¬ÛÀ³ªº¼Ò²Õ¡A¤£¹L§Ú¤ñ¸û¤Ö¥Î¡A¤@­Ó¬O¤£¼ô¡A¤@­Ó¬Opattern¤ñ¸û¼sªº®É­Ô¡A©È·|¤ñ¹ï¨ì·N®Æ¥~ªº¸ê®Æ¡C
python¤£·|ª¾¹D¨º¬OÁ`­¶¼Æ¡AÁÙ¬O­n¾a¤HÆ[¹î¡Apython¥u¯àª¾¹D¬Y­Ó¦ì¤l(©Î²Å¦Xre pattern)ªº¼Æ¦r¬O¤°»ò¡C

TOP

¦^´_ 95# clianghot546
­n¬Ý§A¬O­n¤°»ò¼Ëªºµ²ªG¡C»Ý­n°µ¨º¨Ç³B²z¡C¦pªG­n°µ¤@¨Ç¤@¯ëªº¹Bºâ§Ú³£ÁÙ¬O·|©ñ¨ìexcel¸Ì¡C
ÁöµM²z½×¤Wpython¤]¦³«Ü¦h®M¥ó¥i¥H°µ¦UºØ¤ÀªR¡A¤£¹L¤@¯ë¨Ï¥Î¨Ó»¡¡A§ÚÁÙ¬Oı±oexcelªº¤u§@ªí¸òÀx¦s®æ¤ñ¸û¿Ë¤Á¡C

TOP

¥»©«³Ì«á¥Ñ c_c_lai ©ó 2016-9-12 16:22 ½s¿è

¦^´_ 94# zyzzyva
Python ¸àÄÀªº¹ê¦b¤Ó¬ü¤F¡A²¼ä§ã­n¡C
½Ð°Ý r'/\s\d{1,3} ¥Nªí¤§²[¸q¬°¦ó¡H
BeautifulSoup ¤@©w¬O·f°t 'lxml' ¨Ï¥Î¡H
soup.select('.tb_pages td')[0] ¨ä¤¤ªº [0]  «üªº¬O¡H
¥¦¯àª½±µÀ³¥Î regular expression §â¥¦¨ú¥X¨Ó¹ê¦b¬O¤Ó¼F®`¤F¡C
#94 ¥¦«ç»òª¾¹D­n§ìªº¬OÁ`­¶¼Æ¡H

TOP

·Q½Ð°Ý¦U¦ì³£¬O¥Îpython§ì¨ú§¹ºô¸ô¸ê®Æ«á¦A³z¹Lexcel¾ã²z¡AÁÙ¬O¥i¥H¥Îpython¤@¦¸³B²z¨ì³Ì«á©Ò­nªºµ²ªG¡C

TOP

        ÀR«ä¦Û¦b : ¤Hªº²´·úªø¦b«e­±¡A¥u¬Ý¨ì§O¤Hªº¯ÊÂI¡Aµ·²@¬Ý¤£¨ì¦Û¤vªº¯ÊÂI¡C
ªð¦^¦Cªí ¤W¤@¥DÃD