返回列表 上一主題 發帖

[發問] 請問如何以VBA擷取網站上的資料?

其實你錄製一下就可得到代碼,再把2610改成儲存格位址如[a1], 如此而已。
  1. Sub Macro2()
  2.     With ActiveSheet.QueryTables.Add(Connection:= _
  3.         "URL;http://tw.stock.yahoo.com/d/s/company_" & [a1] & ".html", _
  4.         Destination:=Range("A3"))
  5.         .Name = "company_" & [a1]
  6.         .FieldNames = True
  7.         .RowNumbers = False
  8.         .FillAdjacentFormulas = False
  9.         .PreserveFormatting = True
  10.         .RefreshOnFileOpen = False
  11.         .BackgroundQuery = True
  12.         .RefreshStyle = xlInsertDeleteCells
  13.         .SavePassword = False
  14.         .SaveData = True
  15.         .AdjustColumnWidth = True
  16.         .RefreshPeriod = 0
  17.         .WebSelectionType = xlSpecifiedTables
  18.         .WebFormatting = xlWebFormattingNone
  19.         .WebTables = "9"
  20.         .WebPreFormattedTextToColumns = True
  21.         .WebConsecutiveDelimitersAsOne = True
  22.         .WebSingleBlockTextImport = False
  23.         .WebDisableDateRecognition = False
  24.         .WebDisableRedirections = False
  25.         .Refresh BackgroundQuery:=False
  26.     End With
  27. End Sub
複製代碼

TOP

這是excel的說明中的範例寫法,比錄製得的代碼簡潔多了
  1. Sub qq()
  2. Set shFirstQtr = ActiveSheet
  3. Set qtQtrResults = shFirstQtr.QueryTables _
  4.     .Add(Connection:="URL;http://tw.stock.yahoo.com/d/s/company_" & [a1] & ".html", _
  5.         Destination:=shFirstQtr.Cells(3, 1))
  6. With qtQtrResults
  7.     .WebFormatting = xlNone
  8.     .WebSelectionType = xlSpecifiedTables
  9.     .WebTables = "9"
  10.     .Refresh
  11. End With
  12. End Sub
複製代碼

TOP

謝謝oobird版主大大。
感謝您幫小弟開啟這一扇大門,
同時也幫小弟搬開心中的一塊
大石頭。
一語點醒如此好用的EXCEL
錄製功能,小弟卻將它遺忘。

另小弟想宣告設定
DIM qtQtrResults AS ......
才能讓它在WITH qtQtrResults時
能顯示出其屬性及功能呢?
With qtQtrResults
    .WebFormatting = xlNone
    .WebSelectionType = xlSpecifiedTables
    .WebTables = "9"
    .Refresh
End With

感恩大大!

TOP

Dim qtQtrResults As QueryTable

TOP

謝謝oobird版主大大。
感謝您引領小弟進入
另一個新的領域。

感恩大大!

TOP

Sub qq()
Set shFirstQtr = ActiveSheet
Set qtQtrResults = shFirstQtr.QueryTables _
    .Add(Connection:="URL;http://tw.stock.yahoo.com/d/s/company_" & [a1] & ".html", _
        Destination:=shFirstQtr.Cells(3, 1))
With qtQtrResults
    .WebFormatting = xlNone
    .WebSelectionType = xlSpecifiedTables
    .WebTables = "9"
    .Refresh
End With
End Sub
我想問問那麼在這段代碼中
.Refreshstyle 可以插在那個位置??
因為我想這個是覆蓋資料而不是不斷插入

TOP

已找到.Refreshstyle的位置
但找不到怎樣只導入第一欄的資料@@
VBA新手

TOP

請問 如果只想抓 網站上的部分資料 下來, VBA 要如何寫 ?
  1. Sub 期貨交易口數()
  2. Set shFirstQtr = ActiveSheet
  3. Set qtQtrResults = shFirstQtr.QueryTables _
  4.     .Add(Connection:="URL;http://www.taifex.com.tw/chinese/3/7_12_3_tbl.asp", _
  5.         Destination:=shFirstQtr.Cells(3, 1))
  6. With qtQtrResults
  7.     .WebFormatting = xlNone
  8.     .WebSelectionType = xlSpecifiedTables
  9.     .WebTables = "2"
  10.     .Refresh
  11. End With
  12. End Sub
複製代碼

抓期貨口數.jpg (124 KB)

期貨交易口數

抓期貨口數.jpg

TOP

回復 5# GBKEE

大大請問一下,我用下面這個程式抓出的資料跟原始網站的資料比較下,下面有一段沒有抓不到,是不是有參數沒設好? 要wait嗎?因為下面有顯示Loading more data...但是我不知道怎樣讓他跑完

原始網站: https://finance.yahoo.com/quote/AAPL/history?period1=1473638400&period2=1505174400&interval=1d&filter=history&frequency=1d

Sub QueryTable()
    Const xlURL As String = "https://finance.yahoo.com/quote/AAPL/history?period1=1473638400&period2=1505174400&interval=1d&filter=history&frequency=1d"
    With ActiveSheet.QueryTables.Add("URL;" & xlURL, Destination:=Range("$A$1"))
        .WebFormatting = xlWebFormattingNone
        .TablesOnlyFromHTML = False
        .RefreshStyle = xlOverwriteCells
        .SaveData = True
        .Refresh 0
    End With
End Sub

TOP

回復 5# GBKEE

GBKEE大大,我原本想用這幾天問你的,抓Crumb位置來下載csv檔案,但沒有成功,所以想換下面那個網址試試,但抓出的資料不完整,如果可以抓取完整就太好了
https://finance.yahoo.com/quote/AAPL/history?period1=1473638400&period2=1505174400&interval=1d&filter=history&frequency=1d
紅框下面是要抓的資料:

javascript:;

Data 1.PNG (52.44 KB)

Data 1.PNG

TOP

        靜思自在 : 【生命在呼吸間】佛陀說:「生命在呼吸間。」人無法管住自己的生命,更無法擋住死期,讓自己永住人間。既然生命去來這麼無常,我們更應該好好地愛惜它、利用它、充實它,讓這無常、寶貴的生命,散發它真善美的光輝,映照出生命真正的價值。
返回列表 上一主題