Użyj getElementById na HTMLElement zamiast HTMLDocument

Bawiłem się skrobaniem danych ze stron internetowych za pomocą VBS / VBA.

Gdyby to był Javascript, byłbym tak łatwy, ale nie wydaje się być tak prosty w VBS / VBA.

To jest przykład, który zrobiłem dla odpowiedzi, działa, ale planowałem uzyskać dostęp do węzłów potomnych za pomocągetElementByTagName ale nie mogłem wymyślić, jak ich używać! TheHTMLElement obiekt nie ma tych metod.

Sub Scrape()
Dim Browser As InternetExplorer
Dim Document As HTMLDocument
Dim Elements As IHTMLElementCollection
Dim Element As IHTMLElement

Set Browser = New InternetExplorer

Browser.navigate "http://www.hsbc.com/about-hsbc/leadership"

Do While Browser.Busy And Not Browser.readyState = READYSTATE_COMPLETE
    DoEvents
Loop

Set Document = Browser.Document

Set Elements = Document.getElementsByClassName("profile-col1")

For Each Element in Elements
    Debug.Print "[  name] " & Trim(Element.Children(1).Children(0).innerText)
    Debug.Print "[ title] " & Trim(Element.Children(1).Children(1).innerText)
Next Element

Set Document = Nothing
Set Browser = Nothing
End Sub

Patrzyłem naHTMLElement.document właściwość, czy jest to fragment dokumentu, ale jest trudny do pracy lub po prostu nie jest tym, co myślę

Dim Fragment As HTMLDocument
Set Element = Document.getElementById("example") ' This works
Set Fragment = Element.document ' This doesn't

To również wydaje się być długa droga do tego (chociaż zazwyczaj jest to sposób na vba imo). Ktoś wie, czy istnieje prostszy sposób na funkcje łańcuchowe?

Document.getElementById("target").getElementsByTagName("tr") byłoby super...

questionAnswers(4)

yourAnswerToTheQuestion