關閉廣告
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 這段給刪除
結果發現好像不行,我的慧根好像沒很好 不好意思又要麻煩版主幫我解惑了
想要抓取的資料網址 : 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"看一下
我目前抓到這個值 但是好像沒辦法把它放到特定的位置 他一抓好像就直接抓成很多個 如果我要指定將他放在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
關閉廣告