返回列表 上一主題 發帖

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

本帖最後由 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

本帖最後由 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

本帖最後由 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

回復 12# carzyindex
下載一支股票要將近10分鐘 , 我沒測試完,請你慢慢測試 .
  1. Option Explicit
  2. Sub 明細下載()
  3.     Dim 股票代號 As Range, 日期 As Variant, i As Integer, A, T As Date
  4.     Dim Rng As Range
  5.     Do While Not IsDate(日期)
  6.         日期 = InputBox("輸入查詢日期", "日期", Date)
  7.         If 日期 = "" Then End
  8.     Loop
  9.     日期 = Format(日期, "yyyymmdd")
  10.     Set 股票代號 = Workbooks("TEST.XLSX").Sheets("sheet2").[A1]
  11.     Do While 股票代號.Value <> ""
  12.         T = Time
  13.         With Workbooks("TEST.XLSX").Sheets.Add(After:=Sheets(Sheets.Count))
  14.             .Name = 日期 & "-" & 股票代號     '設立 在工作表名稱
  15.             Application.StatusBar = False
  16.             On Error GoTo A_Wait
  17.             i = 1
  18.             Do
  19.                 Set Rng = .Cells(Rows.Count, 1).End(xlUp).Offset(1)
  20.                 DoEvents
  21.                 With .QueryTables.Add(Connection:="URL;http://bsr.twse.com.tw/bshtm/bshtm_report_Messages.aspx?strDate=" & 日期 & "&StartNumber=" & 股票代號 & "&FocusIndex=" & i, Destination:=Rng)
  22.                     .Name = 日期 & "_" & 股票代號 & "_" & i
  23.                     .WebSelectionType = xlSpecifiedTables
  24.                     .WebFormatting = xlWebFormattingNone
  25.                     .WebTables = "6"
  26.                     .WebPreFormattedTextToColumns = True
  27.                     .WebConsecutiveDelimitersAsOne = True
  28.                     .WebSingleBlockTextImport = False
  29.                     .WebDisableDateRecognition = False
  30.                     .WebDisableRedirections = False
  31.                 ''''''無法查詢時稍待  到  A_Wait: '''''
  32.                     .Refresh BackgroundQuery:=False
  33.                     If Application.CountA(.ResultRange) = 0 Then GoTo Out
  34.                         i = i + 1
  35.                 End With
  36.                 A = CreateObject("WScript.Shell").popup("請等後下載..." & Chr(10) & Chr(10) & "** 請勿按下  [確定] **", 4, 日期 & "_" & 股票代號 & "  第" & i & "頁", 16 * 3 + 0)
  37.                 Application.ScreenUpdating = True
  38.             Loop
  39.             
  40. Out:
  41.             .UsedRange.Columns.AutoFit
  42.             ''''''''刪除 A,F欄的空格  ''''''''''''''''''''''''''''''''
  43.             .Range(.Cells(Rows.Count, "B").End(xlUp).Offset(1), .Cells(Rows.Count, "B")).Offset(, -1).Clear
  44.             .Range(.Cells(Rows.Count, "G").End(xlUp).Offset(1), .Cells(Rows.Count, "G")).Offset(, -1).Clear
  45.             '''''''''''''''''''''''''''''''''''''''
  46.             A = CreateObject("WScript.Shell").popup("共下載" & i & "頁", 5, 日期 & "_" & 股票代號, 48 + 0)
  47.             Application.StatusBar = 股票代號 & " 共下載 " & i & "頁 費時 " & Format(Time - T, "HH:MM:SS")
  48.         End With
  49.         Set 股票代號 = 股票代號.Offset(1)
  50.     Loop
  51.     End
  52. A_Wait:
  53.     Application.StatusBar = "無法查詢等候10秒鐘"   '網頁繁忙時程式會有錯誤
  54.     Application.Wait Now + TimeValue("00:00:10")  '可增加等候查詢的秒數
  55.     Err.Clear
  56.     Application.StatusBar = False
  57.     Resume    '重返查詢
  58. End Sub
複製代碼

TOP

回復 15# carzyindex
有此網頁好辦些,但下載還是很費時的,慢慢的測試吧!!



測試.rar (17.46 KB)

TOP

回復 27# gracyei
Sheets(日期 & "-" & 股票代號).Cells.Clear   

"執行階段錯誤'424':此處需要物件   活頁簿中沒有命名為日期 & "-" & 股票代號 的工作表

日期 & "-" & 股票代號  這工作表所有Cells(儲存格).Clear(清除資料)

TOP

回復 29# gracyei
將檔案上傳看看

TOP

回復 31# gracyei
修改如下
  1. Option Explicit
  2. Sub 個股交易整頁()
  3.     Dim 股票代號 As Range, 日期 As Variant, F As Range
  4.     Do While Not IsDate(日期)
  5.         日期 = InputBox("輸入查詢日期", "日期", Date)
  6.         If 日期 = "" Then End
  7.     Loop
  8.     日期 = Format(日期, "yyyymmdd")
  9.     Set 股票代號 = ThisWorkbook.Sheets("代號").[A1]
  10.     On Error Resume Next                           '不理會 在執行程式的時候發生的錯誤
  11.     Do While 股票代號.Value <> ""
  12.         ThisWorkbook.Sheets.Add after:=Sheets(Sheets.Count)
  13.         ActiveSheet.Name = 日期 & "-" & 股票代號
  14.         Sheets("代號").Activate
  15.         With Sheets(日期 & "-" & 股票代號)
  16.             With .QueryTables.Add(Connection:="URL;http://bsr.twse.com.tw/bshtm/bshtm_report_Messages.aspx?strDate=" & 日期 & "&StartNumber=" & 股票代號 & "&FocusIndex=All_100", Destination:=.Cells(1))
  17.                 .Name = 日期 & "-" & 股票代號
  18.                 .WebSelectionType = xlEntirePage
  19.                 .WebFormatting = xlWebFormattingNone
  20.                 .WebPreFormattedTextToColumns = True
  21.                 .WebConsecutiveDelimitersAsOne = True
  22.                 .WebSingleBlockTextImport = False
  23.                 .WebDisableDateRecognition = False
  24.                 .WebDisableRedirections = False
  25.                 Do
  26.                     DoEvents
  27.                     Err.Clear                         'Err物件的重設
  28.                     .Refresh BackgroundQuery:=False   '查詢失敗 會產生錯誤
  29.                 Loop Until Err.Number = 0             '執行迴圈直到錯誤消:失 查詢成功
  30.             End With
  31.             .Range("F5:H" & Rows.Count).Delete 1
  32.             .Range(.Cells(.Rows.Count, "B").End(xlUp).Offset(1), .Cells(.Rows.Count, "B")).Offset(, -1).Clear
  33.             .Range(.Cells(Rows.Count, "G").End(xlUp).Offset(1), .Cells(Rows.Count, "G")).Offset(, -1).Clear
  34.             Set F = .[A:A].Find("*頁 / 共*", LOOKAT:=xlPart)
  35.             Do While Not F Is Nothing
  36.                 F.Offset(-1).Resize(7).EntireRow.Delete
  37.                 Set F = .[A:A].FindNext
  38.             Loop
  39.             .UsedRange.Columns.AutoFit
  40.         End With
  41.         Set 股票代號 = 股票代號.Offset(1)
  42.     Loop
  43.     MsgBox "下載完畢 !!!", 32 + 0, "個股交易明細"
  44. End Sub
複製代碼

TOP

回復 33# sd-jason

TOP

        靜思自在 : 不要小看自己,因為人有無限的可能。
返回列表 上一主題