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

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

¦^´_ 90# c_c_lai
¤Ó¦n¤F!¦pªG³o¼Ë¤]¤£¦æ´NÀYµh¤F¡C
­ì¤å¤¤¦³´£¨ì§Æ±æ¥i¥H¥Î¿¤¥«¬°³æ¦ì¨Ó§ì¡C¦]¬°ºô¯¸¥»¨­´N¦³´£¨Ñ¿¤¥«§O¤ÀÃþªº³sµ²¡A¦p¡G
http://church.oursweb.net/slocation.php?w=1&c=TW&a=¥x¤¤¥«&t=
¨ä¹ê¥u­n§â³sµ²´«¤@¤U¡AÁ`­¶¼Æ§ï¤@¤U¥Î²{¦³ªº code´N¥i¥H§â¸Ó°Ï°ìªº¸ê®Æ§ì¤U¨Ó¤F¡C
¦ý¦pªG­nÅý¨Ï¥ÎªÌ¿ï¾Ü©O¡H°²³]§Ú­Ì¤w¸g¨Ì¨Ï¥ÎªÌ¿ï¾Ü¶i¤J¸Ó¤À°Ïªº­¶­±¡A¦ý¬O­n¦p¦ó¨M©wÁ`­¶¼Æ©O¡H
¥H¥x¤¤¥«¨Ó»¡¡C Screenshot_1.png ¡A¯à§â¹Ï¸ÌÂŰ餤ªº31§ì¥X¨Ó¶Ü¡H

TOP

¦^´_ 91# zyzzyva
²{¦b½Í¥\¤OÁÙ®t¤Ó»·¤F(ªì²L)¡AÁÙµLªk¾r¶¿©|½Ð«ü¾É¡A
§V¤O¾Ç²ß¤¤¡A§Ú´X¥G±Nªñ¦³¥b¦~¦h¥¼´¿±µÄ² Python¡A
À³¥Î¤è­±ÁÙ©|¥¼§¹¥þ¤W¤â¡A¥¿¦n¸I¤W§A­Ì¤~¨Ï§Ú­«¬B«H¤ß¡C
(«e¥b­Ó¤ë¤~°Ê§¹²´·ú¤â³N) ¯u¦³³Â·Ð§A«ü±Ð¤F¡I

TOP

¦^´_ 92# c_c_lai
»¡«ü¾É¤£´±·í¡A§Ú¤]¬O¶X³o­Ó¾÷·|¤@Ãä°µ¤@Ãä¾Ç¡C
¨ä¹ê¦n¤£¦n§ì¸òºô­¶ªºhtml½s±Æ¦³«Ü¤jªºÃö«Y¡A·|´£³o­Ó¼Æ¦r¬O¦]¬°§Ú¦Û¤v¸Õ¤Fı±o³o­Ó¼Æ¦rÁÙ¦³ÂI¤p³Â·Ð¡C

±z¦pªG¬O¥Îchrome¡A¥i¥H¦b·Q§ì¨úªº¸ê®Æ¤W«ö¥kÁä¡A¿ï¡uÀˬd¡v¡AÀ³¸Ó¥i¥H¬Ý¨ìÃþ¦ü¹Ï¤¤ªºµe­±(¦³®É­Ô»Ý­nÂI¶}¾ðª¬µ²ºc)¡C
¥H³o­Óµ²ºc¨Ó»¡¡A°£¤F¤W­±tableªºclass="tb_pages"¡A§Ú¨S¦³¬Ý¨ì¤°»ò®e©ö¥ÎªºªF¦è¡A©Ò¥H§Ú·|¿ï³o­Ótable°µ¬°°_©lªº°Ñ¦ÒÂI¡C
(³q±`§ä¨ì¥Ø¼Ðªº¤è¦¡³£·|¦³«D±`¦hºØ¡A¥u­n¯à§ä±o¨ì´N¦n¤F)¡A¥Î¤U­±ªºcode´N¥i¥H§ä¨ì¾ã­Ó¥y¤l¡G
  1. url = 'http://church.oursweb.net/slocation.php?w=1&c=TW&a=¥x¤¤¥«&t='

  2. res = requests.get(url)

  3. res.encoding='utf-8'

  4. soup = BeautifulSoup(res.text, 'lxml')

  5. target = soup.select('.tb_pages td')[0].text

  6. print(target)
½Æ»s¥N½X
¦ý¬O¼Æ¦rÁÙ¬OÂæb¸ÌÀY¡A¯u¬O¦³ÂI·Ð¤H¡C
¨ì³oÃä§Ú¬O¦A¥Îregular expression§â¥¦¨ú¥X¨Ó¡C
  1. import re
  2. total_num = re.search(r'/\s\d{1,3}', target).group().replace('/ ','')
  3. print(total_num)
½Æ»s¥N½X
¥t¤@­Ó¤ñ¸û²³æªº¤è¦¡«h¬O¥Îlxml¡C

TOP

­è·Q¤F¤@¤U¡A¥H³o­Ó­¶­±¨Ó»¡¡A¨ä¹ê¤]¤£¥Î¥Î¨ìBeautifulSoup¡Aª½±µ¥Îre´N¥i¥H¤F¡C
  1. import requests
  2. import re

  3. url = 'http://church.oursweb.net/slocation.php?w=1&c=TW&a=¥x¤¤¥«&t='

  4. res = requests.get(url)

  5. total_num = re.findall(r'/\s\d{1,3}', res.text)[0].replace('/ ','')

  6. print(total_num)
½Æ»s¥N½X

TOP

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

¦^´_ 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

¦^´_ 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

¦^´_ 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

¦^´_ 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

        ÀR«ä¦Û¦b : ¦³¦h¤Ö¤O¶q´N°µ¦h¤Ö¨Æ¡A¤£­n¤ß¦sµ¥«Ý¡Aµ¥«Ý¤~·|¸¨ªÅ¡C
ªð¦^¦Cªí ¤W¤@¥DÃD