返回列表 上一主題 發帖

[發問] 網頁資料下載簡化

[發問] 網頁資料下載簡化

本帖最後由 carzyindex 於 2011-5-21 09:26 編輯

這個網站比較有人性一點  參數找的到也可以設定

但是我不知道頁數有幾張

土法煉鋼一頁一頁弄也很沒有效率

查詢太多次還會被檔

請問程式該如何下載

例如1101

     2201

<span id="sp_ListCount">54</span>

用 page_num = document.getElementById("sp_ListCount").innerText

就知道頁數了...

不過查詢大多次會被擋,用VBA程式一樣會被擋.....
diabo

TOP

本帖最後由 GBKEE 於 2011-5-1 10:46 編輯

回復 1# carzyindex
PS: 今日(5/1)測試此網頁 只允許連續下載10頁
  1. Sub Ex()
  2.     Dim 股票代號 As String, 日期  As Variant, N, i As Integer
  3.     Do While Not IsDate(日期)
  4.         日期 = InputBox("輸入查詢日期", "日期", Date)
  5.         If 日期 = "" Then End
  6.     Loop
  7.     Do While 股票代號 = ""
  8.         股票代號 = InputBox("股票代號", "輸入查詢之股票代號", "1101")
  9.         If 日期 = "" Then End
  10.     Loop
  11.     日期 = Format(日期, "yyyymmdd")
  12.     With ActiveSheet
  13.         For Each N In .Names
  14.             N.Delete
  15.         Next
  16.         .Cells.Clear
  17.         Application.ScreenUpdating = False
  18.         With .QueryTables.Add(Connection:="URL;http://bsr.twse.com.tw/bshtm/bshtm_report_Messages.aspx?strDate=" & 日期 & "&StartNumber=" & 股票代號 & "&FocusIndex=1", Destination:=Range("A1"))
  19.             .Name = 日期 & "_" & 股票代號 & "_1"
  20.             .FieldNames = True
  21.             .RowNumbers = False
  22.             .FillAdjacentFormulas = False
  23.             .PreserveFormatting = True
  24.             .RefreshOnFileOpen = False
  25.             .BackgroundQuery = True
  26.             .RefreshStyle = xlInsertDeleteCells
  27.             .SavePassword = False
  28.             .SaveData = True
  29.             .AdjustColumnWidth = True
  30.             .RefreshPeriod = 0
  31.             .WebSelectionType = xlEntirePage
  32.             .WebFormatting = xlWebFormattingNone
  33.             .WebPreFormattedTextToColumns = True
  34.             .WebConsecutiveDelimitersAsOne = True
  35.             .WebSingleBlockTextImport = False
  36.             .WebDisableDateRecognition = False
  37.             .WebDisableRedirections = False
  38.             .Refresh BackgroundQuery:=False
  39.             If Application.CountA(.ResultRange) = 0 Then
  40.                 MsgBox Format(日期, "0000/00/00") & " 休市!!!  或  股票代號:" & 股票代號 & " 錯誤 !!!"
  41.                 [A1].Select
  42.                 End
  43.             End If
  44.         End With
  45.         i = 2
  46.         Do
  47.             .Cells(Rows.Count, 1).End(xlUp).Offset(1).Select
  48.             With .QueryTables.Add(Connection:="URL;http://bsr.twse.com.tw/bshtm/bshtm_report_Messages.aspx?strDate=" & 日期 & "&StartNumber=" & 股票代號 & "&FocusIndex=" & i, Destination:=Selection)
  49.                 .Name = 日期 & "_" & 股票代號 & "_" & i
  50.                 .WebSelectionType = xlSpecifiedTables
  51.                 .WebFormatting = xlWebFormattingNone
  52.                 .WebTables = "6"
  53.                 .WebPreFormattedTextToColumns = True
  54.                 .WebConsecutiveDelimitersAsOne = True
  55.                 .WebSingleBlockTextImport = False
  56.                 .WebDisableDateRecognition = False
  57.                 .WebDisableRedirections = False
  58.                 .Refresh BackgroundQuery:=False
  59.                 If .ResultRange(1) Like "ip*" Then
  60.                     .ResultRange.Clear
  61.                     GoTo Out
  62.                 End If
  63.                 i = i + 1
  64.             End With
  65.         Loop
  66. Out:
  67.         .UsedRange.Columns.AutoFit
  68.         .[A1].Select
  69.     End With
  70.     Application.ScreenUpdating = True
  71. End Sub
複製代碼

TOP

本帖最後由 carzyindex 於 2011-5-3 08:20 編輯

回復 3# GBKEE


    感謝各位大大的幫忙

請問可以批次10頁10頁的下載嗎

TOP

本帖最後由 GBKEE 於 2011-5-4 09:15 編輯

回復 4# carzyindex
請耐心等候  程式在跑時 **請勿按下** [確定]

'此網頁有下載管制 須有下載時間間隔 3秒 可全部下載完成
A = CreateObject("WScript.Shell").popup("請等待4秒後下載" & Chr(10) & Chr(10) & "** 請勿按下 ** [確定]", 3, 日期 & "_" & .[F2] & "  第" & i & "頁", 16 * 3 + 0)
  1. Sub 個股交易明細下載()
  2.     Dim 股票代號 As String, 日期 As Variant, N, i As Integer, A, T As Date
  3.     Do While Not IsDate(日期)
  4.         日期 = InputBox("輸入查詢日期", "日期", Date)
  5.         If 日期 = "" Then End
  6.     Loop
  7.     Do While 股票代號 = ""
  8.         股票代號 = InputBox("股票代號", "輸入查詢之股票代號", "1101")
  9.         If 日期 = "" Then End
  10.     Loop
  11.     日期 = Format(日期, "yyyymmdd")
  12.     T = Time
  13.     With ActiveSheet
  14.         For Each N In .Names
  15.             N.Delete
  16.         Next
  17.         .Cells.Clear
  18.         Application.StatusBar = False
  19.         On Error GoTo A_Wait
  20.         With .QueryTables.Add(Connection:="URL;http://bsr.twse.com.tw/bshtm/bshtm_report_Messages.aspx?strDate=" & 日期 & "&StartNumber=" & 股票代號 & "&FocusIndex=1", Destination:=Range("A1"))
  21.             .Name = 日期 & "_" & 股票代號 & "_1"
  22.             .FieldNames = True
  23.             .RowNumbers = False
  24.             .FillAdjacentFormulas = False
  25.             .PreserveFormatting = True
  26.             .RefreshOnFileOpen = False
  27.             .BackgroundQuery = True
  28.             .RefreshStyle = xlInsertDeleteCells
  29.             .SavePassword = False
  30.             .SaveData = True
  31.             .AdjustColumnWidth = True
  32.             .RefreshPeriod = 0
  33.             .WebSelectionType = xlEntirePage
  34.             .WebFormatting = xlWebFormattingNone
  35.             .WebPreFormattedTextToColumns = True
  36.             .WebConsecutiveDelimitersAsOne = True
  37.             .WebSingleBlockTextImport = False
  38.             .WebDisableDateRecognition = False
  39.             .WebDisableRedirections = False
  40.             .Refresh BackgroundQuery:=False
  41.             If Application.CountA(.ResultRange) = 0 Then
  42.                 MsgBox Format(日期, "0000/00/00") & " 休市!!!  或  股票代號:" & 股票代號 & " 錯誤 !!!"
  43.                 [A1].Select
  44.                 End
  45.             End If
  46.         End With
  47.         i = 2
  48.         Do
  49.             .Cells(Rows.Count, 1).End(xlUp).Offset(1).Select
  50.             With .QueryTables.Add(Connection:="URL;http://bsr.twse.com.tw/bshtm/bshtm_report_Messages.aspx?strDate=" & 日期 & "&StartNumber=" & 股票代號 & "&FocusIndex=" & i, Destination:=Selection)
  51.                 .Name = 日期 & "_" & 股票代號 & "_" & i
  52.                 .WebSelectionType = xlSpecifiedTables
  53.                 .WebFormatting = xlWebFormattingNone
  54.                 .WebTables = "6"
  55.                 .WebPreFormattedTextToColumns = True
  56.                 .WebConsecutiveDelimitersAsOne = True
  57.                 .WebSingleBlockTextImport = False
  58.                 .WebDisableDateRecognition = False
  59.                 .WebDisableRedirections = False
  60.               ''''''無法查詢時稍待  到  A_Wait: '''''
  61.                 .Refresh BackgroundQuery:=False
  62.                 If Application.CountA(.ResultRange) = 0 Then GoTo Out
  63.                 i = i + 1
  64.             End With
  65.             A = CreateObject("WScript.Shell").popup("請等後下載..." & Chr(10) & Chr(10) & "** 請勿按下 ** [確定]", 4, 日期 & "_" & .[F2] & "  第" & i & "頁", 16 * 3 + 0)
  66.             Application.ScreenUpdating = True
  67.         Loop
  68. Out:
  69.         .UsedRange.Columns.AutoFit
  70.         .[A1].Select
  71.         A = CreateObject("WScript.Shell").popup("共下載" & i & "頁", 5, 日期 & "_" & 股票代號, 48 + 0)
  72.         Application.StatusBar = "共下載 " & i & "頁 費時 " & Format(Time - T, "HH:MM:SS")
  73.     End With
  74.     End
  75. A_Wait:
  76.     Application.StatusBar = "無法查詢等候10秒鐘"
  77.     Application.Wait Now + TimeValue("00:00:10")
  78.     Err.Clear
  79.     Application.StatusBar = False
  80.     Resume    '重返查詢
  81. End Sub
複製代碼

TOP

回復 5# GBKEE


    感謝版大再次相助,非常感謝.

TOP

回復 5# GBKEE


看來問題比我想像中還大的樣子

第一頁資料位置偏移

今天早上測試出現error 408

載到第六頁就掛掉了

無奈我只會排版的vba對網頁的不知道從何下手

TOP

本帖最後由 GBKEE 於 2011-5-4 19:57 編輯

回復 7# carzyindex
5樓的程式剛才測試是有些不順 已稍為修改了
第一頁資料位置偏移
  1. Sub 簡易明細下載()
  2.     Dim 股票代號 As String, 日期 As Variant, N, i As Integer, A, T As Date
  3.     Do While Not IsDate(日期)
  4.         日期 = InputBox("輸入查詢日期", "日期", Date)
  5.         If 日期 = "" Then End
  6.     Loop
  7.     Do While 股票代號 = ""
  8.         股票代號 = InputBox("股票代號", "輸入查詢之股票代號", "1101")
  9.         If 日期 = "" Then End
  10.     Loop
  11.     日期 = Format(日期, "yyyymmdd")
  12.     T = Time
  13.     With ActiveSheet
  14.         For Each N In .Names
  15.             N.Delete
  16.         Next
  17.         .Cells.Clear
  18.         Application.StatusBar = False
  19.         On Error GoTo A_Wait
  20.         i = 1
  21.         Do
  22.             .Cells(Rows.Count, 1).End(xlUp).Offset(1).Select
  23.             With .QueryTables.Add(Connection:="URL;http://bsr.twse.com.tw/bshtm/bshtm_report_Messages.aspx?strDate=" & 日期 & "&StartNumber=" & 股票代號 & "&FocusIndex=" & i, Destination:=Selection)
  24.                 .Name = 日期 & "_" & 股票代號 & "_" & i
  25.                 .WebSelectionType = xlSpecifiedTables
  26.                 .WebFormatting = xlWebFormattingNone
  27.                 .WebTables = "6"
  28.                 .WebPreFormattedTextToColumns = True
  29.                 .WebConsecutiveDelimitersAsOne = True
  30.                 .WebSingleBlockTextImport = False
  31.                 .WebDisableDateRecognition = False
  32.                 .WebDisableRedirections = False
  33.               ''''''無法查詢時稍待  到  A_Wait: '''''
  34.                 .Refresh BackgroundQuery:=False
  35.                 If Application.CountA(.ResultRange) = 0 Then GoTo Out
  36.                 i = i + 1
  37.             End With
  38.             A = CreateObject("WScript.Shell").popup("請等後下載..." & Chr(10) & Chr(10) & "** 請勿按下  [確定] **", 4, 日期 & "_" & 股票代號 & "  第" & i & "頁", 16 * 3 + 0)
  39.             Application.ScreenUpdating = True
  40.         Loop
  41. Out:
  42.         .UsedRange.Columns.AutoFit
  43.         .[A1].Select
  44.         A = CreateObject("WScript.Shell").popup("共下載" & i & "頁", 5, 日期 & "_" & 股票代號, 48 + 0)
  45.         Application.StatusBar = 股票代號 &" 共下載 " & i & "頁 費時 " & Format(Time - T, "HH:MM:SS")
  46.     End With
  47.     End
  48. A_Wait:
  49.     Application.StatusBar = "無法查詢等候5秒鐘"
  50.     Application.Wait Now + TimeValue("00:00:05")
  51.     Err.Clear
  52.     Application.StatusBar = False
  53.     Resume    '重返查詢
  54. End Sub
複製代碼

TOP

回復 9# carzyindex
這是另一領域 我功力不夠 也找不出來阿!

TOP

回復 10# GBKEE


    感謝版大回覆

TOP

        靜思自在 : 我們要做好社會的環保,也要做好內心的環保。
返回列表 上一主題