返回列表 上一主題 發帖

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

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

With CreateObject("InternetExplorer.application")           
        .Visible = True                                       
        .Navigate "http://www.gretai.org.tw/web/stock/aftertrading/broker_trading/brokerBS.php?l=zh-tw"
      
        Do Until .ReadyState = 4               
            DoEvents
        Loop
Set img = .document.all.tags("img")(0)
end with
請問我如何把img這個圖存成c:\2.png

太強了...我正好想破頭了T_T...沒想到有大大分享解決辦法^^

TOP

回復 35# GBKEE


如果用 .responseText
怎麼寫,達到 IF ...Then...ElseIF.... EndIf

TOP

回復 34# HSIEN6001
  1.   With stream   '在這With下
  2.                .Type = 1
  3.                 .Open
  4.                 .write xml.responseBody
  5.                 '**** 判斷讀取資料   ****************************************************************
  6.                 If .document.body.Innertext Like "*該日無交易資訊*" Then
  7.                  '*******  .document 是IE的子物件******
  8.                     GoTo NN  'Goto 下一個
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

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

回復 32# HSIEN6001
  1.     '***** 判斷讀取資料  End *************************************************************
  2.             End If  '沒有With 少這 End If
複製代碼
但這程式尚有缺失需修改.

  1. Set Rng = [C:C].Find("NG", , , xlWhole)  '找不時的處置???
  2. For Each 代號 In Range(Rng.Offset(0, -2), [E65536].End(xlUp)) '須在前面
  3.         With CreateObject("InternetExplorer.application") '在For Each 代號 迴圈內
  4.          
  5.          '少掉查詢按鍵
  6.          if ....
  7.          '這段 IF 判斷要放在
  8.        End If   

  9.        沒有dir檢查 存檔之檔案是否存在
  10.          .SaveToFile (path & 代號 & ".csv")
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

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

回復 29# GBKEE

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

TOP

回復 25# flask

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

TOP

本帖最後由 GBKEE 於 2014-12-16 22:02 編輯

回復 28# HSIEN6001
還有 Workbooks.Open 可用
  1. Option Explicit
  2. Public IE As Object, Msg As Boolean
  3. Const 圖形 = "d:\驗證圖.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.           CSV載入
  20.             Target = ""
  21.             Application.EnableEvents = True
  22.         End If
  23.     End With
  24. End Sub

  25. Private Sub Get_Ie()
  26.     Set IE = CreateObject("InternetExplorer.Application")
  27.     With IE
  28.         .Visible = True
  29.         '券商買賣證券日報表查詢系統(一般交易)
  30.         .Navigate "http://www.gretai.org.tw/web/stock/aftertrading/broker_trading/brokerBS.php?l=zh-tw"
  31.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  32.     End With
  33. End Sub

  34. Private Sub 圖形更新()
  35.     If IE Is Nothing Then Get_Ie
  36.     If Msg Then MsgBox "驗證圖 更新完畢"
  37.     Msg = False
  38.     With IE
  39.         .Refresh
  40.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  41.         網路圖片存檔 .Document.all.tags("IMG")(0).href
  42.     End With
  43.     Sheet1.Shapes("驗證圖").Fill.UserPicture 圖形    '
  44. End Sub

  45. Private Sub 網路圖片存檔(img As String)
  46.     Dim xml As Object     '用來取得網頁資料
  47.     Dim stream            'As ADODB.stream   '用來儲存二進位檔案
  48.     Set xml = CreateObject("Microsoft.XMLHTTP")
  49.     Set stream = CreateObject("ADODB.stream")
  50.     xml.Open "GET", img, 0
  51.     xml.send
  52.     With stream
  53.         .Open
  54.         .Type = 1
  55.         .write xml.responseBody
  56.         If Dir(圖形) <> "" Then Kill 圖形
  57.         .SaveToFile (圖形)
  58.         .Close
  59.     End With
  60. End Sub

  61. Private Sub CSV載入()
  62.     Dim e As Object, A As Object, kDate As String, S As String
  63.     Application.EnableEvents = True
  64.     If IE Is Nothing Then
  65.         圖形更新
  66.         MsgBox "驗證圖已更新"
  67.         Exit Sub
  68.     End If
  69.     With IE
  70.         .Document.all.tags("INPUT")("stk_code").Value = Range(證券代號)
  71.         .Document.all.tags("INPUT")("auth_num").Value = Trim(Range(驗證碼))
  72.         Set A = .Document.all.tags("BUTTON")
  73.         For Each e In A
  74.             If Trim(e.Innertext) = "查詢" And e.ID = "" Then
  75.             e.Click
  76.             Exit For
  77.             End If
  78.         Next
  79.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  80.         If .Document.body.Innertext Like "***該股票該日無交易資訊***" Then S = "***該股票該日無交易資訊***"
  81.         If .Document.body.Innertext Like "***驗證碼錯誤,請重新查詢。***" Then S = "***驗證碼錯誤,請重新查詢。*** "
  82.         If S <> "" Then
  83.             'Range("a" & k + 1) = S
  84.             MsgBox S
  85.             GoTo NN
  86.         End If
  87.         kDate = Format(Date, "emmdd")
  88.         Application.DisplayAlerts = False
  89.         With Workbooks.Open("http://www.gretai.org.tw/web/stock/aftertrading/broker_trading/download_ALLCSV.php?curstk=" & Range(證券代號) & "&stk_date=" & kDate & "&&auth=" & Range(驗證碼))
  90.             .SaveAs "D:\" & Range(證券代號)
  91.             With .Sheets(1)
  92.                 .UsedRange.Range("G:K").SpecialCells(xlCellTypeConstants).Offset(1).Copy .Cells(.Rows.Count, 1).End(xlUp).Offset(1)
  93.                 .UsedRange.Range("G:K").Clear
  94.             End With
  95.             .Close True
  96.         End With
  97.         Application.DisplayAlerts = True
  98.         MsgBox "證券代號 : " & Range(證券代號) & " CSV 載入完畢"
  99. NN:
  100.         .Quit
  101.     End With
  102.     Set IE = Nothing
  103.     圖形更新
  104. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 君子如水,隨方就圓,無處不自在。
返回列表 上一主題