返回列表 上一主題 發帖

請問如何將網頁的圖片存檔

回復 14# GBKEE



請問,該如何載入其他頁次
若不用寫入Excel,而直接 .Click 下載CSV檔(BIG5) 檔案
語法該怎麼修正
  1.         Set aa = .Document.getElementsByTAGName("INPUT")
  2.         For R = 0 To aa.Length - 1
  3.             If aa(R).Value = "下載CSV檔(BIG5)" And aa(R).Type = "button" Then
  4.                 aa(R).Click   '有 "下載CSV檔(BIG5)" 的按鈕
  5.                     Do While .Busy Or .ReadyState <> 4: DoEvents: Loop
  6.             End If
  7.         Next
複製代碼

TOP

回復 19# GBKEE

耶!成了

謝謝您的指導!
程式語言,博大精深
對我來說很艱難,因為好用,還是很努力的盡量多吸收一些
還沒看懂的部分,也先收集起來,待日後消化

~感恩~

TOP

回復 21# joey0415

整段語法是這樣的 , 綜合 #2 & #9
ThisWorkbook模組的程式碼
  1. Option Explicit
  2. Private Sub Workbook_Open()
  3.     Sheet1.Msg = True
  4.     Run "Sheet1.圖形更新"
  5. End Sub
  6. Private Sub Workbook_BeforeClose(Cancel As Boolean)
  7.     On Error Resume Next
  8.     If Not Sheet1.IE Is Nothing Then Sheet1.IE.Quit
  9. End Sub
複製代碼
Sheet1模組的程式碼
  1. Option Explicit
  2. Public IE As Object, Msg As Boolean, IEx As Object
  3. Const 圖形 = "C:\驗證圖.jpg"
  4. Const 證券代號 = "F2"
  5. Const 驗證碼 = "F4"
  6. Private Sub Worksheet_Change(ByVal Target As Range)
  7.     Range(證券代號).Interior.ColorIndex = IIf(Range(證券代號).Value = "", 2, 36)
  8.     With Target.Cells(1)
  9.          If .Address(0, 0) = 驗證碼 Then .Interior.ColorIndex = IIf(Len(Trim(.Cells)) = 5, 36, 2)
  10.          If .Address(0, 0) = 驗證碼 And Len(Trim(.Cells)) = 5 And Range(證券代號).Value <> "" Then
  11.             If IE Is Nothing Then
  12.                 Target = ""
  13.                 Msg = True
  14.                 圖形更新
  15.                 Exit Sub
  16.             End If
  17.             Application.EnableEvents = False
  18.             日報表載入
  19.             Target = ""
  20.             Application.EnableEvents = True
  21.         End If
  22.     End With
  23. End Sub
  24. Private Sub 日報表載入()
  25.     Dim e As Object, A As Object, k  As Integer, i As Integer, S As String
  26.     If IE Is Nothing Then
  27.         圖形更新
  28.         MsgBox "驗證圖已更新"
  29.         Exit Sub
  30.     End If
  31.     With IE
  32.         .Document.all.tags("INPUT")("stk_code").Value = Range(證券代號)
  33.         .Document.all.tags("INPUT")("auth_num").Value = Trim(Range(驗證碼))
  34.         Set A = .Document.all.tags("BUTTON")
  35.         For Each e In A
  36.             If Trim(e.Innertext) = "查詢" And e.ID = "" Then
  37.             e.Click
  38.             Exit For
  39.             End If
  40.         Next
  41.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  42.         UsedRange.Offset(6).Clear
  43.         Range("a" & k + 1) = S
  44.         If .Document.body.Innertext Like "***該股票該日無交易資訊***" Then S = "***該股票該日無交易資訊***"
  45.         If .Document.body.Innertext Like "***驗證碼錯誤,請重新查詢。***" Then S = "***驗證碼錯誤,請重新查詢。*** "
  46.         If S <> "" Then
  47.             Range("a" & k + 1) = S
  48.             MsgBox S
  49.             GoTo NN
  50.         End If
  51.         Set IEx = CreateObject("InternetExplorer.Application")
  52.         IEx.Navigate "about:Tabs"
  53.         Set A = .Document.all.tags("A")
  54.         單頁載入 .Document.all.tags("table")(0).outerHTML
  55.         [A6].Select
  56.         '********程式碼寫在工作表模組: Me 指這工作表模組
  57.         Me.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
  58.         '****************************************
  59.         If A.Length = 459 Then
  60.             For i = 2 To 3
  61.                 單頁載入 .Document.all.tags("table")(i).outerHTML
  62.                 With Range("A" & Rows.Count).End(xlUp).Offset(1)
  63.                     .Select
  64.                     Me.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
  65.                 End With
  66.             Next
  67.         Else
  68.             For k = 0 To A.Length - 1
  69.                 If Val(A(k).Innertext) >= 1 Then
  70.                     Debug.Print A(k).Innertext
  71.                     A(k).Click
  72.                     Do While .Busy Or .readyState <> 4: DoEvents: Loop
  73.                     Set A = .Document.all.tags("A")
  74.                     Do While .Busy Or .readyState <> 4: DoEvents: Loop
  75.                     For i = 2 To 3
  76.                         單頁載入 .Document.all.tags("table")(i).outerHTML
  77.                         With Range("A" & Rows.Count).End(xlUp).Offset(1)
  78.                             .Select
  79.                             Me.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:=False, NoHTMLFormatting:=True
  80.                         End With
  81.                     Next
  82.                 End If
  83.             Next
  84.         End If
  85.         IEx.Quit
  86.         Set IEx = Nothing
  87.         整理
  88. NN:
  89.         .Quit
  90.     End With
  91.     Set IE = Nothing
  92.     圖形更新
  93. End Sub
  94. Private Sub 單頁載入(S)
  95.     With IEx
  96.         .Document.body.innerHTML = S
  97.         .ExecWB 17, 2       '  Select All
  98.         .ExecWB 12, 2       '  Copy selection
  99.     End With
  100. End Sub
  101. Private Sub 整理()
  102.     On Error Resume Next
  103.     Application.EnableEvents = False
  104.     With UsedRange.Offset(10)
  105.         .Replace "序號", "=ex", xlWhole
  106.         .SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Delete
  107.     End With
  108.     UsedRange(1).Select
  109.     Application.EnableEvents = True
  110. End Sub
  111. Private Sub Get_Ie()
  112.     Set IE = CreateObject("InternetExplorer.Application")
  113.     With IE
  114.        ' .Visible = True
  115.         '券商買賣證券日報表查詢系統(一般交易)
  116.         .Navigate "http://www.gretai.org.tw/web/stock/aftertrading/broker_trading/brokerBS.php?l=zh-tw"
  117.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  118.     End With
  119. End Sub
  120. Private Sub 圖形更新()
  121.     If IE Is Nothing Then Get_Ie
  122.     If Msg Then MsgBox "驗證圖 更新完畢"
  123.     Msg = False
  124.     With IE
  125.         .Refresh
  126.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  127.         網路圖片存檔 .Document.all.tags("IMG")(0).href
  128.     End With
  129.     Sheet1.Shapes("驗證圖").Fill.UserPicture 圖形    '
  130. End Sub
  131. Private Sub 網路圖片存檔(img As String)
  132.     Dim xml As Object     '用來取得網頁資料
  133.     Dim stream            'As ADODB.stream   '用來儲存二進位檔案
  134.     Set xml = CreateObject("Microsoft.XMLHTTP")
  135.     Set stream = CreateObject("ADODB.stream")
  136.     xml.Open "GET", img, 0
  137.     xml.send
  138.     With stream
  139.         .Open
  140.         .Type = 1
  141.         .write xml.ResponseBody
  142.         If Dir(圖形) <> "" Then Kill 圖形
  143.         .SaveToFile (圖形)
  144.         .Close
  145.     End With
  146. End Sub
複製代碼

TOP

本帖最後由 HSIEN6001 於 2014-12-16 15:06 編輯

回復 19# GBKEE

您好,我已經找到CSV下載的參數
GET
URL = "http://www.gretai.org.tw/web/stock/aftertrading/broker_trading/download_ALLCSV.php?curstk=" & 證券代號 & "&stk_date=" & 日期 & "&auth=" & 驗證碼

我把表身匯入改成這段
  1.     Dim xml As Object     '用來取得網頁資料
  2.     Dim stream            'As ADODB.stream   '用來儲存二進位檔案
  3.     Set xml = CreateObject("Microsoft.XMLHTTP")
  4.     Set stream = CreateObject("ADODB.stream")

  5. Dim path As String, url
  6. path = "C:\"
  7.     Dim 日期 As String
  8.     日期 = "1031215"

  9. 'GET http://www.gretai.org.tw/web/stock/aftertrading/broker_trading/download_ALLCSV.php?curstk=股票代號&stk_date=日期&auth=驗證碼

  10.     url = "http://www.gretai.org.tw/web/stock/aftertrading/broker_trading/download_ALLCSV.php?curstk=" & Sheets("Sheet1").[F2] & "&stk_date=" & 日期 & "&auth=" & Sheets("Sheet1").[F4]
  11.         xml.Open "GET", url, 0
  12.         xml.send
  13.     With stream
  14.         .Type = 1
  15.         .Open
  16.         .write xml.responseBody
  17.         If Dir(path & Sheets("Sheet1").[F2] & ".csv") <> "" Then Kill (path & Sheets("Sheet1").[F2] & ".csv")
  18.         .SaveToFile (path & Sheets("Sheet1").[F2] & ".csv")
  19.         .Close
  20.     End With
複製代碼
非常謝謝您的指導!!
^__^

TOP

回復 25# flask

   .Columns("A:A").TextToColumns Destination:=.Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
        :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
        Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1)), _
        TrailingMinusNumbers:=True

    請問這段怎麼應用,我也想要zip檔
可否給參考,謝謝

TOP

回復 24# joey0415

存檔位置自行修正
參考!
    下載CSV.rar (10 KB)

TOP

回復 25# flask

我已經看懂了,太厲害了
真的很快!!謝謝

TOP

回復 29# GBKEE

版大 Workbook open 匯入CSV的方式 , 收下囉!謝謝~

TOP

回復 29# GBKEE
您好,這裡一直出現缺 With 不知道哪裡要修改
麻煩幫忙校正問題
  1. Sub 下載CSV()
  2. Dim path As String, 日期 As String, URL As String, 代號
  3. path = "C:\myStock\"
  4. 日期 = Format(Date, "emmdd")
  5. Dim Rng As Range
  6. AA:
  7.     Set Rng = [C:C].Find("NG", , , xlWhole)
  8.     Sheets("下載依據").Activate
  9.     With CreateObject("InternetExplorer.application")
  10.         .Visible = True
  11.         .navigate "http://www.gretai.org.tw/web/stock/aftertrading/broker_trading/brokerBS.php?l=zh-tw"
  12.             Do Until .readyState = 4
  13.                 DoEvents
  14.             Loop
  15.         驗證碼 = InputBox("輸入查詢驗證碼", "驗證碼", code)   '驗證碼 = InputBox
  16.         For Each 代號 In Range(Rng.Offset(0, -2), [E65536].End(xlUp))
  17.             .document.all("stk_code").Value = 代號
  18.             .document.all("auth_num").Value = 驗證碼
  19.         '**** 直接下載CSV ****
  20.         Dim xml As Object
  21.         Dim stream
  22.         Set xml = CreateObject("Microsoft.XMLHTTP")
  23.         Set stream = CreateObject("ADODB.stream")
  24.             'GET http://www.gretai.org.tw/web/stock/aftertrading/broker_trading/download_ALLCSV.php?curstk=8069&stk_date=1031215&auth=驗證碼
  25.             URL = "http://www.gretai.org.tw/web/stock/aftertrading/broker_trading/download_ALLCSV.php?curstk=" & 代號 & "&stk_date=" & 日期 & "&auth=" & 驗證碼
  26.                 xml.Open "GET", URL, 0
  27.                 xml.send
  28.             With stream
  29.                 .Type = 1
  30.                 .Open
  31.                 .write xml.responseBody
  32.                 '**** 判斷讀取資料   ****************************************************************
  33.                 If .document.body.Innertext Like "*該日無交易資訊*" Then
  34.                     GoTo NN  'Goto 下一個
  35.                 ElseIf .document.body.Innertext Like "*驗證碼錯誤,請重新查詢*" Then
  36.                     Sheets("下載依據").Range("C:C").Delete      '清除舊的NG記號
  37.                     代號.Offset(, 2) = "NG"                     '新增斷點
  38.                     .Quit   '關閉 IE 視窗
  39.                     GoTo AA
  40.                  ElseIf .document.body.Innertext Like "*驗證碼已逾期,請重新查詢*" Then
  41.                     Sheets("下載依據").Range("C:C").Delete      '清除舊的NG記號
  42.                     代號.Offset(, 2) = "NG"                     '新增斷點
  43.                     .Quit   '關閉 IE 視窗
  44.                     GoTo AA
  45.                 '***** 判斷讀取資料  End *************************************************************
  46.                 .SaveToFile (path & 代號 & ".csv")
  47.                 .Close
  48.             End With
  49. NN:
  50.     Next
  51.         .Quit   '關閉 IE 視窗
  52.     End With
  53.     Range("C2") = "NG"  '重新作記號,方便下回下載
  54. End Sub
複製代碼

TOP

回復 33# GBKEE


    出現執行階段錯誤'438 , 物件不支援此屬性或方法
跟這一段判斷式有關,不知道該怎麼改 ?
沒判斷內容,會下載到空值
  1.                 .write xml.responseBody
  2.                 '**** 判斷讀取資料   ****************************************************************
  3.                 If .document.body.Innertext Like "*該日無交易資訊*" Then
  4.                     GoTo NN  'Goto 下一個
  5.                 ElseIf .document.body.Innertext Like "*驗證碼錯誤,請重新查詢*" Then
  6.                      '代號.Offset(, 2) = "NG"
  7.                      GoTo AA ''視窗不關閉 & 重新輸入驗證碼 & 代號
  8.                 ElseIf .document.body.Innertext Like "*驗證碼已逾期,請重新查詢*" Then
  9.                     Sheets("下載依據").Range("F:F").Delete      '清除舊的NG記號
  10.                     代號.Offset(, 1) = "NG"                     '在隔壁 第1欄位標註 "NG" , 方便重新抓取時,避開已經下載的資料
  11.                     .Quit   '關閉 IE 視窗
  12.                      GoTo AA ''視窗重新開啟 & 重新輸入驗證碼 & 代號
  13.                 End If
  14.                 '***** 判斷讀取資料  End *************************************************************
複製代碼

TOP

        靜思自在 : 並非有錢魷是快樂,問心無愧心最安。
返回列表 上一主題