2016-08-29 309 views
-1

我需要提取以下HTML標記的d屬性:提取數據

<g class="highcharts-series highcharts-tracker" visibility="visible" zIndex="0.1" transform="translate(1,26) scale(1 1)" style="" clip-path="url(#highcharts-5)"><path fill="#fe3232" d="M 1.5 168.5 L 1.5 109.5 L 5.5 109.5 L 5.5 168.5 Z M 3.5 109.5 L 3.5 109.5 M 3.5 168.5 L 3.5 188" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 9.5 171.5 L 9.5 130.5 L 13.5 130.5 L 13.5 171.5 Z M 11.5 130.5 L 11.5 127 M 11.5 171.5 L 11.5 197" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 18.5 141.5 L 18.5 130.5 L 22.5 130.5 L 22.5 141.5 Z M 20.5 130.5 L 20.5 121 M 20.5 141.5 L 20.5 147" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 26.5 138.5 L 26.5 42.5 L 30.5 42.5 L 30.5 138.5 Z M 28.5 42.5 L 28.5 33 M 28.5 138.5 L 28.5 144" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 34.5 42.5 L 34.5 28.5 L 38.5 28.5 L 38.5 42.5 Z M 36.5 28.5 L 36.5 13 M 36.5 42.5 L 36.5 45" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 43.5 45.5 L 43.5 28.5 L 47.5 28.5 L 47.5 45.5 Z M 45.5 28.5 L 45.5 25 M 45.5 45.5 L 45.5 54" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 51.5 45.5 L 51.5 42.5 L 55.5 42.5 L 55.5 45.5 Z M 53.5 42.5 L 53.5 28 M 53.5 45.5 L 53.5 45.5" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 59.5 45.5 L 59.5 42.5 L 63.5 42.5 L 63.5 45.5 Z M 61.5 42.5 L 61.5 39 M 61.5 45.5 L 61.5 63" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 68.5 54.5 L 68.5 45.5 L 72.5 45.5 L 72.5 54.5 Z M 70.5 45.5 L 70.5 45.5 M 70.5 54.5 L 70.5 60" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 76.5 54.5 L 76.5 45.5 L 80.5 45.5 L 80.5 54.5 Z M 78.5 45.5 L 78.5 45.5 M 78.5 54.5 L 78.5 60" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 84.5 48.5 L 84.5 45.5 L 88.5 45.5 L 88.5 48.5 Z M 86.5 45.5 L 86.5 39 M 86.5 48.5 L 86.5 51" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 93.5 48.5 L 93.5 45.5 L 97.5 45.5 L 97.5 48.5 Z M 95.5 45.5 L 95.5 45.5 M 95.5 48.5 L 95.5 51" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 101.5 45.5 L 101.5 39.5 L 105.5 39.5 L 105.5 45.5 Z M 103.5 39.5 L 103.5 33 M 103.5 45.5 L 103.5 51" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 109.5 36.5 L 109.5 33.5 L 113.5 33.5 L 113.5 36.5 Z M 111.5 33.5 L 111.5 22 M 111.5 36.5 L 111.5 42" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 118.5 42.5 L 118.5 33.5 L 122.5 33.5 L 122.5 42.5 Z M 120.5 33.5 L 120.5 28 M 120.5 42.5 L 120.5 45" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 126.5 48.5 L 126.5 39.5 L 130.5 39.5 L 130.5 48.5 Z M 128.5 39.5 L 128.5 39.5 M 128.5 48.5 L 128.5 48.5" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 134.5 39.5 L 134.5 36.5 L 138.5 36.5 L 138.5 39.5 Z M 136.5 36.5 L 136.5 36.5 M 136.5 39.5 L 136.5 42" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 143.5 42.5 L 143.5 39.5 L 147.5 39.5 L 147.5 42.5 Z M 145.5 39.5 L 145.5 39.5 M 145.5 42.5 L 145.5 42.5" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 151.5 42.5 L 151.5 39.5 L 155.5 39.5 L 155.5 42.5 Z M 153.5 39.5 L 153.5 39.5 M 153.5 42.5 L 153.5 42.5" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 159.5 42.5 L 159.5 39.5 L 163.5 39.5 L 163.5 42.5 Z M 161.5 39.5 L 161.5 39.5 M 161.5 42.5 L 161.5 42.5" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 168.5 45.5 L 168.5 42.5 L 172.5 42.5 L 172.5 45.5 Z M 170.5 42.5 L 170.5 42.5 M 170.5 45.5 L 170.5 48" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 176.5 42.5 L 176.5 39.5 L 180.5 39.5 L 180.5 42.5 Z M 178.5 39.5 L 178.5 39.5 M 178.5 42.5 L 178.5 45" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 184.5 39.5 L 184.5 33.5 L 188.5 33.5 L 188.5 39.5 Z M 186.5 33.5 L 186.5 33.5 M 186.5 39.5 L 186.5 45" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 193.5 39.5 L 193.5 33.5 L 197.5 33.5 L 197.5 39.5 Z M 195.5 33.5 L 195.5 33.5 M 195.5 39.5 L 195.5 39.5" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 201.5 48.5 L 201.5 30.5 L 205.5 30.5 L 205.5 48.5 Z M 203.5 30.5 L 203.5 30.5 M 203.5 48.5 L 203.5 51" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 209.5 60.5 L 209.5 45.5 L 213.5 45.5 L 213.5 60.5 Z M 211.5 45.5 L 211.5 45.5 M 211.5 60.5 L 211.5 63" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 218.5 65.5 L 218.5 63.5 L 222.5 63.5 L 222.5 65.5 Z M 220.5 63.5 L 220.5 57 M 220.5 65.5 L 220.5 68" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 226.5 65.5 L 226.5 57.5 L 230.5 57.5 L 230.5 65.5 Z M 228.5 57.5 L 228.5 54 M 228.5 65.5 L 228.5 65.5" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 234.5 60.5 L 234.5 54.5 L 238.5 54.5 L 238.5 60.5 Z M 236.5 54.5 L 236.5 51 M 236.5 60.5 L 236.5 68" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 243.5 63.5 L 243.5 57.5 L 247.5 57.5 L 247.5 63.5 Z M 245.5 57.5 L 245.5 54 M 245.5 63.5 L 245.5 63.5" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 251.5 54.5 L 251.5 54.5 L 255.5 54.5 L 255.5 54.5 Z M 253.5 54.5 L 253.5 54.5 M 253.5 54.5 L 253.5 60" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 259.5 57.5 L 259.5 57.5 L 263.5 57.5 L 263.5 57.5 Z M 261.5 57.5 L 261.5 51 M 261.5 57.5 L 261.5 60" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 268.5 63.5 L 268.5 54.5 L 272.5 54.5 L 272.5 63.5 Z M 270.5 54.5 L 270.5 54.5 M 270.5 63.5 L 270.5 63.5" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 276.5 68.5 L 276.5 63.5 L 280.5 63.5 L 280.5 68.5 Z M 278.5 63.5 L 278.5 57 M 278.5 68.5 L 278.5 68.5" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 284.5 71.5 L 284.5 68.5 L 288.5 68.5 L 288.5 71.5 Z M 286.5 68.5 L 286.5 60 M 286.5 71.5 L 286.5 74" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 293.5 74.5 L 293.5 71.5 L 297.5 71.5 L 297.5 74.5 Z M 295.5 71.5 L 295.5 65 M 295.5 74.5 L 295.5 74.5" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 301.5 74.5 L 301.5 65.5 L 305.5 65.5 L 305.5 74.5 Z M 303.5 65.5 L 303.5 65.5 M 303.5 74.5 L 303.5 74.5" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 309.5 65.5 L 309.5 63.5 L 313.5 63.5 L 313.5 65.5 Z M 311.5 63.5 L 311.5 63.5 M 311.5 65.5 L 311.5 68" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 318.5 65.5 L 318.5 63.5 L 322.5 63.5 L 322.5 65.5 Z M 320.5 63.5 L 320.5 63.5 M 320.5 65.5 L 320.5 65.5" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 326.5 71.5 L 326.5 68.5 L 330.5 68.5 L 330.5 71.5 Z M 328.5 68.5 L 328.5 68.5 M 328.5 71.5 L 328.5 71.5" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 334.5 74.5 L 334.5 57.5 L 338.5 57.5 L 338.5 74.5 Z M 336.5 57.5 L 336.5 54 M 336.5 74.5 L 336.5 74.5" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 343.5 54.5 L 343.5 54.5 L 347.5 54.5 L 347.5 54.5 Z M 345.5 54.5 L 345.5 51 M 345.5 54.5 L 345.5 54.5" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 351.5 57.5 L 351.5 57.5 L 355.5 57.5 L 355.5 57.5 Z M 353.5 57.5 L 353.5 51 M 353.5 57.5 L 353.5 60" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 359.5 63.5 L 359.5 60.5 L 363.5 60.5 L 363.5 63.5 Z M 361.5 60.5 L 361.5 60.5 M 361.5 63.5 L 361.5 65" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 368.5 65.5 L 368.5 63.5 L 372.5 63.5 L 372.5 65.5 Z M 370.5 63.5 L 370.5 63.5 M 370.5 65.5 L 370.5 71" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 376.5 71.5 L 376.5 65.5 L 380.5 65.5 L 380.5 71.5 Z M 378.5 65.5 L 378.5 60 M 378.5 71.5 L 378.5 71.5" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 384.5 71.5 L 384.5 63.5 L 388.5 63.5 L 388.5 71.5 Z M 386.5 63.5 L 386.5 60 M 386.5 71.5 L 386.5 77" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 393.5 74.5 L 393.5 65.5 L 397.5 65.5 L 397.5 74.5 Z M 395.5 65.5 L 395.5 63 M 395.5 74.5 L 395.5 77" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 401.5 83.5 L 401.5 77.5 L 405.5 77.5 L 405.5 83.5 Z M 403.5 77.5 L 403.5 77.5 M 403.5 83.5 L 403.5 92" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 409.5 83.5 L 409.5 71.5 L 413.5 71.5 L 413.5 83.5 Z M 411.5 71.5 L 411.5 71.5 M 411.5 83.5 L 411.5 86" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 418.5 74.5 L 418.5 42.5 L 422.5 42.5 L 422.5 74.5 Z M 420.5 42.5 L 420.5 22 M 420.5 74.5 L 420.5 86" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 426.5 45.5 L 426.5 42.5 L 430.5 42.5 L 430.5 45.5 Z M 428.5 42.5 L 428.5 36 M 428.5 45.5 L 428.5 60" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 434.5 39.5 L 434.5 39.5 L 438.5 39.5 L 438.5 39.5 Z M 436.5 39.5 L 436.5 33 M 436.5 39.5 L 436.5 54" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 443.5 42.5 L 443.5 33.5 L 447.5 33.5 L 447.5 42.5 Z M 445.5 33.5 L 445.5 10 M 445.5 42.5 L 445.5 51" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 451.5 83.5 L 451.5 36.5 L 455.5 36.5 L 455.5 83.5 Z M 453.5 36.5 L 453.5 36.5 M 453.5 83.5 L 453.5 101" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 459.5 89.5 L 459.5 83.5 L 463.5 83.5 L 463.5 89.5 Z M 461.5 83.5 L 461.5 77 M 461.5 89.5 L 461.5 101" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 468.5 101.5 L 468.5 89.5 L 472.5 89.5 L 472.5 101.5 Z M 470.5 89.5 L 470.5 86 M 470.5 101.5 L 470.5 124" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 476.5 103.5 L 476.5 103.5 L 480.5 103.5 L 480.5 103.5 Z M 478.5 103.5 L 478.5 89 M 478.5 103.5 L 478.5 109" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 484.5 124.5 L 484.5 106.5 L 488.5 106.5 L 488.5 124.5 Z M 486.5 106.5 L 486.5 101 M 486.5 124.5 L 486.5 136" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 493.5 127.5 L 493.5 106.5 L 497.5 106.5 L 497.5 127.5 Z M 495.5 106.5 L 495.5 103 M 495.5 127.5 L 495.5 133" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 501.5 103.5 L 501.5 95.5 L 505.5 95.5 L 505.5 103.5 Z M 503.5 95.5 L 503.5 92 M 503.5 103.5 L 503.5 109" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 509.5 86.5 L 509.5 86.5 L 513.5 86.5 L 513.5 86.5 Z M 511.5 86.5 L 511.5 80 M 511.5 86.5 L 511.5 95" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 518.5 92.5 L 518.5 86.5 L 522.5 86.5 L 522.5 92.5 Z M 520.5 86.5 L 520.5 83 M 520.5 92.5 L 520.5 101" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 526.5 89.5 L 526.5 80.5 L 530.5 80.5 L 530.5 89.5 Z M 528.5 80.5 L 528.5 77 M 528.5 89.5 L 528.5 106" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 534.5 77.5 L 534.5 77.5 L 538.5 77.5 L 538.5 77.5 Z M 536.5 77.5 L 536.5 71 M 536.5 77.5 L 536.5 95" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 543.5 89.5 L 543.5 74.5 L 547.5 74.5 L 547.5 89.5 Z M 545.5 74.5 L 545.5 74.5 M 545.5 89.5 L 545.5 103" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 551.5 98.5 L 551.5 89.5 L 555.5 89.5 L 555.5 98.5 Z M 553.5 89.5 L 553.5 86 M 553.5 98.5 L 553.5 115" stroke="black" stroke-width="1"></path><path fill="#fe3232" d="M 559.5 106.5 L 559.5 95.5 L 563.5 95.5 L 563.5 106.5 Z M 561.5 95.5 L 561.5 89 M 561.5 106.5 L 561.5 109" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 568.5 106.5 L 568.5 98.5 L 572.5 98.5 L 572.5 106.5 Z M 570.5 98.5 L 570.5 98.5 M 570.5 106.5 L 570.5 115" stroke="black" stroke-width="1"></path><path fill="#32ea32" d="M 576.5 103.5 L 576.5 95.5 L 580.5 95.5 L 580.5 103.5 Z M 578.5 95.5 L 578.5 86 M 578.5 103.5 L 578.5 103.5" stroke="black" stroke-width="1"></path></g> 

我用下面的VBA代碼嘗試,但我得到錯誤強調文本

Sub scraping() 

    Set ie = CreateObject("InternetExplorer.Application") 
    ie.Visible = False 
    ie.navigate "http:\\website.com" 

    Set elems = ie.Document.getElementsByClassName("highcharts-series highcharts-tracker") 
    Set e = elems.getElementsByTagName("path") 

    k = 1 
    Do Until k = e.Length 
     Cells(k, 5) = e.getAttribute("d") 
     k = k + 1 
    Loop 

End Sub 

我做錯了什麼?

+0

「我在*強調的文本*上出現錯誤」 - 什麼錯誤?什麼強調文字?請[編輯]您的帖子澄清。 –

回答

1

getElementsByClassName返回一組元素(可能包含很多,一個或沒有成員)。

該集合雖然沒有方法getElementsByTagName - 您需要遍歷集合中的元素並在每個元素上調用該方法。

如果您希望只有一個命中,那麼你可以改用:

Set e = elems.getElementsByTagName("path")(0) 
0

您可以用它來讓您的網站的「庫存」。

Sub DumpData() 

Set IE = CreateObject("InternetExplorer.Application") 
IE.Visible = True 

URL = "http://finance.yahoo.com/q?s=sbux&ql=1" 

'Wait for site to fully load 
IE.Navigate2 URL 
Do While IE.Busy = True 
    DoEvents 
Loop 

RowCount = 1 

With Sheets("Sheet1") 
    .Cells.ClearContents 
    RowCount = 1 
    For Each itm In IE.document.all 
     .Range("A" & RowCount) = itm.tagname 
     .Range("B" & RowCount) = itm.ID 
     .Range("C" & RowCount) = itm.classname 
     .Range("D" & RowCount) = Left(itm.innertext, 1024) 

     RowCount = RowCount + 1 
    Next itm 
End With 
End Sub 

另外,考慮閱讀一個網站的所有內容到內存中,然後解析出你需要/想要的文本。

Sub Test() 
    Dim IE As Object 

    Set IE = CreateObject("InternetExplorer.Application") 
    With IE 
     .Visible = True 
     .Navigate "http://www.anything.com/" ' should work for any URL 
     Do Until .ReadyState = 4: DoEvents: Loop 

      x = .document.body.innertext 
      x = Replace(x, Chr(10), Chr(13)) 
      x = Split(x, Chr(13)) 

      .Quit 
     End With 

End Sub