關閉廣告

(不定期更新)使用VBA解決 excel web 查詢無法匯入、匯入太慢的股市資料

好了解 我再研究研究 謝謝版主的回應
不好意思 版主我想再請教您一個問題

HTMLsourcecode.body.innerhtml = .responsetext
Set Table = HTMLsourcecode.all.tags("table")(8 - 1).Rows
For i = 0 To Table.Length - 1
For j = 0 To Table(i).Cells.Length - 1
ActiveSheet.Cells(i + 1, j + 1) = Table(i).Cells(j).innertext
Cells(1, 1) = Table(13).Cells(8).innertext

我現在抓到我要的資料位置了 那因為SET Table = HTMLsourcecode.all.tags 這段語法 是抓取整個表格的

那我現在只需要Cells(1, 1) = Table(13).Cells(8).innertext這一段的語法

一開始我的想法想說 把語法改成

HTMLsourcecode.body.innerhtml = .responsetext
ActiveSheet.Cells(i + 1, j + 1) = Table(i).Cells(j).innertext
Cells(1, 1) = Table(13).Cells(8).innertext
For i = 0 To Table.Length - 1
For j = 0 To Table(i).Cells.Length - 1

然後將SET Table = HTMLsourcecode.all.tags 這段給刪除

結果發現好像不行,我的慧根好像沒很好 不好意思又要麻煩版主幫我解惑了
clothk73713 wrote:
將SET Table = HTMLsourcecode.all.tags 這段給刪除...(恕刪)


不行的,這樣表格資料,就只是網頁原始碼而己(如圖)



就變成要用 td tr去算位置,很麻煩的

或是把整個.responsetext,當成一個字串,用split()找關鍵字,切割2次
例如,要取出"一二三"
a$="abcdddasdf一二三123456"
b$=split(split(a$,"asdf")(1),"123")(0)

有些網頁也會把資料多放一份到id裡面(例如:190樓範例中的日期)
可以改用.getElementbyid()取出資料
哇 ! 版主真的很感謝您 我再來研究看看 哈哈 那網站的原始編碼裡面我有找到id

我再來試試看 感恩您 版主 不好意思 一直麻煩您
我研究了好久還是用不好 版大 我又來愚蠢的發問了

想要抓取的資料網址 : https://www.fangraphs.com/statss.aspx?playerid=10603&position=P

對不起版大這樣真的滿像伸手牌的,我已經絞盡腦汁還是寫錯一堆

Sub getstock()
Dim URL, HTMLsourcecode, GetXml
Set HTMLsourcecode = CreateObject("htmlfile")
Set GetXml = CreateObject("msxml2.xmlhttp")
URL = "https://www.fangraphs.com/statss.aspx?playerid=10603&position=P#winprobability"
With GetXml
.Open "GET", URL, False
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send

HTMLsourcecode.body[removed] = .responsetext
Set Table = HTMLsourcecode.all.tags("table")(8 - 1).Rows
For i = 0 To Table.Length - 1
For j = 0 To Table(i).Cells.Length - 1
ActiveSheet.Cells(i + 1, j + 1) = Table(i).Cells(j).innertext
Next j
Next i
End With
Set HTMLsourcecode = Nothing
Set GetXml = Nothing
End Sub


目前這樣抓就抓出一整個表格




那如果我只要2018那一欄的話 編碼是不是以下這張圖




而版主大大您說的getElementbyid 我去google了一下,他給了這一個範例

<html>
<head>

</head>
<body>

This is a header


Click on the header to alert its value



</body>
</html>

我目前對原始碼不太了解 大概能推出前三個 版主可以幫我看看這樣推對不對嗎

<html> = http://www.w3.org/1999/xhtml

<head> = Head1

<script type="text/javascript">="https://bam.nr-data.net/1/8c8459e5ba?a=2284934&v=1099.d27c17c&to=ZlMHMEtVDUdTW0ZQC18ZJDdpGxBAU0xBSkpQRRUc&rst=8071&ref=https://www.fangraphs.com/statss.aspx&ap=155&be=2036&fe=8037&dc=3678&perf=%7B%22timing%22:%7B%22of%22:1544117904217,%22n%22:0,%22f%22:721,%22dn%22:721,%22dne%22:721,%22c%22:721,%22ce%22:721,%22rq%22:726,%22rp%22:1368,%22rpe%22:2452,%22dl%22:2024,%22di%22:3677,%22ds%22:3677,%22de%22:3790,%22dc%22:8036,%22l%22:8036,%22le%22:8040%7D,%22navigation%22:%7B%7D%7D&jsonp=NREUM.setToken"





clothk73713 wrote:
那如果我只要2018那一欄的話 編碼是不是以下這張圖
...(恕刪)


對的,這個 id 沒錯
但是需使用innerhtml,配合vba剪貼薄功能
或是innerhtml,配合split()
格式才不會跑掉

建議用table方式,直接指定位置就好


clothk73713 wrote:
您說的getElementbyid 我去google了一下,他給了這一個範例
...(恕刪)


那串原始碼,跟您要抓的資料無關
而且 getElementbyid 不是用來拿 header資料的


請改 google "getElementbyid vba"
有興趣順便把 "getelementsbytagname vba"看一下





snare wrote:



對的,這個 i...(恕刪)

好的謝謝版主
哇 樓主我終於明白了 我會了 萬分感恩
版主不好意思 我想再問個問題




我目前抓到這個值 但是好像沒辦法把它放到特定的位置 他一抓好像就直接抓成很多個 如果我要指定將他放在A1不要圖裡面那麼多

這是我目前的語法

Sub getstock()
Dim URL, HTMLsourcecode, GetXml
Set HTMLsourcecode = CreateObject("htmlfile")
Set GetXml = CreateObject("msxml2.xmlhttp")
URL = "https://www.fangraphs.com/statss.aspx?playerid=10603&position=P#winprobability"
With GetXml
.Open "GET", URL, False
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send

HTMLsourcecode.body.innerhtml = .responsetext
Set Table = HTMLsourcecode.all.tags("table")(7).Rows
For i = 0 To Table.Length - 1
For j = 0 To Table(i).Cells.Length - 1
ActiveSheet.Cells(i + 1, j + 1) = Table(6).Cells(8).innertext
Cells(1, 20) = Table(6).Cells(8).innertext
'TABLE(橫欄)CELLS(直列)
Next j
Next i
End With
Set HTMLsourcecode = Nothing
Set GetXml = Nothing
End Sub

阿 我又自己摸索出來了 我會了版主大大 不好意思 像個白癡一樣一直問些基本問題
關閉廣告
文章分享
評分
評分
複製連結
請輸入您要前往的頁數(1 ~ 144)

今日熱門文章 網友點擊推薦!