返回列表 上一主題 發帖

[發問] 如何找到正確的網址,讓EXCEL匯入WEB查詢

[發問] 如何找到正確的網址,讓EXCEL匯入WEB查詢

本帖最後由 bioleon69 於 2016-9-24 18:38 編輯

http://www.cmoney.tw/finance/f00026.aspx?s=2330
想要把此網址匯入,用EXCEL的web查詢
(基本資料那份表格)
結果,失敗

用fiddler找到get網址
http://www.cmoney.tw/finance/ashx/mainpage.ashx?action=GetStockListLatestSaleData&stockId=2330&cmkey=XYlSc5pnmuVR2Cz27paqpg%3D%3D&_=1474712478440
一樣,失敗

如何找到正確網址,再把2330改成變數
用iqy方式匯入excel


以下為匯入成功案例的iqy檔案 (YAHOO股市)
ok.rar (271 Bytes)

回復 10# bioleon69
  1. Option Explicit
  2. Sub Ex_網頁元素()
  3.     Dim i As Integer, xlTab As Object, R As Integer, C As Integer, Surl$
  4.     If Sheets("工作表1").[A1] = "" Then MsgBox "請輸入正確股票代號": Exit Sub
  5.     Surl = "http://www.cmoney.tw/finance/f00026.aspx?s=" & Sheets("工作表1").[A1]
  6.     With CreateObject("InternetExplorer.Application")
  7.         .Visible = False
  8.         .Navigate Surl
  9.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  10.             Set xlTab = .Document.ALL.tags("TABLE")(0)
  11.         If Not xlTab Is Nothing Then
  12.             With ActiveSheet
  13.                 .UsedRange.Clear
  14.                 For R = 0 To xlTab.Rows.Length - 1
  15.                     For C = 0 To xlTab.Rows(R).Cells.Length - 1
  16.                         .Cells(R + 1, C + 1) = xlTab.Rows(R).Cells(C).innertext
  17.                     Next
  18.                 Next
  19.             End With
  20.         Else
  21.             MsgBox "請輸入正確股票代號"
  22.         End If
  23.         .Quit        '關閉網頁
  24.     End With
  25. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 bioleon69 於 2016-10-5 01:31 編輯

感謝GBK以及lai兩位大大
都成功了~~在請教一下
我想加入在A1如果沒有輸入正確變數
或者輸入錯誤
B1會呈現 請輸入正確股票代號
下面是有那裡有問題??
我加入了這幾串程式碼

On Error Resume Next
        .Refresh False
        [B1] = ""
        If Err <> 0 Then
            [B1] = "請輸入正確股票代號"
        Else
        End If
  1. Option Explicit
  2. Sub Ex_網頁元素()
  3.     Dim i As Integer, xlTab As Object, R As Integer, C As Integer, Surl$
  4.         Surl = "http://www.cmoney.tw/finance/f00026.aspx?s=" & Sheets("工作表1").[A1]
  5.     With CreateObject("InternetExplorer.Application")
  6.         .Visible = False
  7.         .Navigate Surl
  8.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  9.         Set xlTab = .Document.ALL.tags("TABLE")(0)
  10.         With ActiveSheet
  11.             .UsedRange.Clear
  12.             For R = 0 To xlTab.Rows.Length - 1
  13.                 For C = 0 To xlTab.Rows(R).Cells.Length - 1
  14.                     .Cells(R + 1, C + 1) = xlTab.Rows(R).Cells(C).innertext
  15.          On Error Resume Next
  16.         .Refresh False
  17.         [B1] = ""
  18.         If Err <> 0 Then
  19.             [B1] = "請輸入正確股票代號"
  20.         Else
  21.         End If
  22.                 Next
  23.             Next
  24.         End With
  25.         .Quit        '關閉網頁
  26.     End With
  27. End Sub
複製代碼

TOP

回復 8# bioleon69
  1.                 For C = 0 To xlTab.Rows(R).Cells.Length - 1
  2.                     .Cells(R + 1, C + 1) = xlTab.Rows(R).Cells(C).innertext
  3.                 Next
複製代碼
調整為
  1.                 For C = 0 To xlTab.Rows(R).Cells.Length - 1
  2.                     .Cells(R + 3, C + 1) = xlTab.Rows(R).Cells(C).innertext
  3.                 Next
複製代碼

TOP

回復 7# c_c_lai
nice!!感謝
在請問一下
要讓這些資料從A3開始輸出 要怎麼修改?

TOP

回復 6# bioleon69
原 6# 樓
  1.     Surl = "http://www.cmoney.tw/finance/f00026.aspx?s=& [A1].Value &"
  2.     With CreateObject("InternetExplorer.Application")
  3.         .Visible = True
  4.         .Navigate " "
複製代碼
修正為:
  1.     Surl = "http://www.cmoney.tw/finance/f00026.aspx?s=" & Sheets("XXX").[A1]
  2.     With CreateObject("InternetExplorer.Application")
  3.         .Visible = True
  4.         .Navigate Surl
複製代碼
即可。

TOP

  1. Option Explicit
  2. Sub Ex_網頁元素()
  3.     Dim i As Integer, xlTab As Object, R As Integer, C As Integer, Surl$
  4.     Surl = "http://www.cmoney.tw/finance/f00026.aspx?s=& [A1].Value &"
  5.     With CreateObject("InternetExplorer.Application")
  6.         .Visible = True
  7.         .Navigate " "
  8.        ' .Navigate "http://www.cmoney.tw/finance/f00026.aspx?s=" & 變數
  9.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  10.         Set xlTab = .Document.ALL.tags("TABLE")(0)
  11.         With ActiveSheet
  12.             .UsedRange.Clear
  13.             For R = 0 To xlTab.Rows.Length - 1
  14.                 For C = 0 To xlTab.Rows(R).Cells.Length - 1
  15.                     .Cells(R + 1, C + 1) = xlTab.Rows(R).Cells(C).innertext
  16.                 Next
  17.             Next
  18.         End With
  19.         .Quit        '關閉網頁
  20.     End With
  21. End Sub
複製代碼
請問一下~~
.Navigate " "這邊要怎麼插入定義的變數surl

TOP

回復 4# bioleon69
  1. Option Explicit
  2. Sub Ex_網頁元素()
  3.     Dim i As Integer, xlTab As Object, R As Integer, C As Integer
  4.     With CreateObject("InternetExplorer.Application")
  5.         .Visible = True
  6.         .Navigate "http://www.cmoney.tw/finance/f00026.aspx?s=2330"
  7.        ' .Navigate "http://www.cmoney.tw/finance/f00026.aspx?s=" & 變數
  8.         Do While .Busy Or .readyState <> 4: DoEvents: Loop      
  9.         Set xlTab = .Document.ALL.tags("TABLE")(0)
  10.         With ActiveSheet
  11.             .UsedRange.Clear
  12.             For R = 0 To xlTab.Rows.Length - 1
  13.                 For C = 0 To xlTab.Rows(R).Cells.Length - 1
  14.                     .Cells(R + 1, C + 1) = xlTab.Rows(R).Cells(C).innertext
  15.                 Next
  16.             Next
  17.         End With
  18.         .Quit        '關閉網頁
  19.     End With
  20. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

有大俠知道如何抓取嗎!!!!
感謝

TOP

有點聽不太懂XDD

TOP

        靜思自在 : 君子為目標,小人為目的。
返回列表 上一主題