PDA

View Full Version : VBA getElementsByTagName Issues



Makeshft
08-30-2013, 12:52 PM
Hello,

I am trying to acquaint myself with VBA's getElementsByTagName, but I am struggling to make it work for me. I use Microsoft Office 2013, and have enabled Microsoft Internet Control and Microsoft HTML Object Library. In the following code, I am trying to grab the birthdate and place it in cell A1. However, this code seems to do nothing. It does not seem to even produce an error.


Sub Extract_TD_text()

Dim URL As String
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim TDelements As IHTMLElementCollection
Dim TDelement As HTMLTableCell
Dim r As Long

'Saved from www vbaexpress com/forum/forumdisplay.php?f=17
URL = [Wikipedia page for Rihanna (not allowed to paste HTML here)]

Set IE = New InternetExplorer

With IE
.navigate URL
.Visible = True

'Wait for page to load
While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend

Set HTMLdoc = .document
End With

Set TDelements = HTMLdoc.getElementsByTagName("TD")


r = 0
For Each TDelement In TDelements
'Look for required TD elements - this check is specific to VBA Express forum - modify as required
If TDelement.className = "infobox.vcard.plainlist" Then
Sheet1.Range("A1").Offset(r, 0).Value = TDelement.innerText
r = r + 1
End If
Next
IE.Quit

End Sub



Thanks for your time.

Teeroy
08-30-2013, 09:28 PM
Hi Makeshft,

Welcome to the forum.

Getting HTML info can be tricky. You need to delve into the page to understand its structure to get the right information back. In your case the information you really need is in the element after that whose className = "persondata-label". The following should produce a Table with the information that you want.


Dim URL As String
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim TDelements As IHTMLElementCollection
Dim TDelement As HTMLTableCell
Dim r As Long
Dim bGetNext As Boolean

'Saved from www vbaexpress com/forum/forumdisplay.php?f=17
URL = "h**p://en.wikipedia.org/wiki/Rihanna" 'change the ** to tt

Set IE = New InternetExplorer

With IE
.navigate URL
.Visible = False

'Wait for page to load
While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend

Set HTMLdoc = .document
End With

Set TDelements = HTMLdoc.getElementsByTagName("TD")


r = 0
For Each TDelement In TDelements
If bGetNext Then
Sheet1.Range("A1").Offset(r - 1, 1).Value = TDelement.innerText
bGetNext = False
End If
'Look for required TD elements - this check is specific to VBA Express forum - modify as required
'Debug.Print TDelement.className '- useful for inspecting the HTML info
If TDelement.className = "persondata-label" Then
Sheet1.Range("A1").Offset(r, 0).Value = TDelement.innerText
bGetNext = True 'Trigger to get the information from the next TDelement
r = r + 1
End If
Next
IE.Quit

End Sub



Hope this helps.

Makeshft
08-31-2013, 09:10 AM
Thanks a ton Teeroy!

Makeshft
08-31-2013, 10:12 AM
Sorry, but I'm still having a little trouble figuring out how you knew to look for the class name "persondata-label." I've been fiddling with Firefox inspect tools for a while now, and the only info I can find about the data is the "span-class" info for each piece of info. Is there anyway I could search by span class or other specific information (e.g. divclass)? If this question is a little too broad, is there any material you suggest I read to understand these functions better?

Thanks again!

snb
08-31-2013, 12:44 PM
Sub M_snb()
With New InternetExplorer
.Navigate "http://en.wikipedia.org/wiki/Rihanna"

Do
DoEvents
Loop Until .ReadyState = 4

For Each it In .Document.getElementsByTagName("TD")
If it.className = "persondata-label" Then c00 = c00 & "|" & it.innerText & "_" & it.className
Next
End With

sn = Split(Mid(c00, 2), "|")
Cells(1).Resize(UBound(sn) + 1) = Application.Transpose(sn)
End Sub

Aurelius M
05-24-2020, 01:46 PM
I need help from the smart guys here on the site. The code snippet below don't give any errors and it looks that it works fine, but the TD element collection stays empty and the innerText is empty too. That while the data is showing in a visual browser. GetelementsbyTagName don't catch TDelements, and I can't figure out why ....


Sub Extract_TD_Text()
Dim URL As String
Dim wMatrix As String
Dim Search As String
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim myTable As HTMLObjectElement
Dim TDelements As IHTMLElementCollection
Dim TDelement As HTMLTableCell
Dim rij As Long
Dim bGetNext As Boolean

URL = "https://www.nasdaq.com/market-activity/stocks/arch"
wMatrix = "sector;industry;1 year target;forward p/e 1 yr.;earnings per share(eps);annualized dividend;ex dividend date;beta"

Set IE = New InternetExplorer
With IE
.Navigate2 URL
.Visible = False 'or True it don't matter
'Wait for page to load
While .Busy Or .ReadyState <> READYSTATE_COMPLETE: DoEvents: Wend
rij = 0
bGetNext = False
Set HTMLdoc = .Document
If HTMLdoc.getElementsByTagName("table")(0).getAttribute("class") = "summary-data__table" Then
Set myTable = HTMLdoc.getElementsByTagName("table")(0)
Set TDelements = myTable.getElementsByTagName("td")
For Each TDelement In TDelements
Search = LCase(Trim(TDelement.innerText))
If Search = "" Then
Search = "Nothing Found"
Else
Search = "*" & Search & "*"
End If
If bGetNext = True Then
Debug.Print TDelement.innerText 'Catch the data from the TDelement
bGetNext = False
End If
'Debug.Print TDelement.className '- useful for inspecting the HTML info
If wMatrix Like Search Then
Debug.Print TDelement.innerText 'Show the string where searching for
bGetNext = True 'Trigger to catch the data from the next TDelement
End If
rij = rij + 1
Next
End If
End With
IE.Quit
End Sub



Thanks in advance.

Paul_Hossler
05-24-2020, 02:06 PM
Welcome to the forum

Take a minute and read the FAQs in the link in my signature

Now ...

That was a 7 year old post. It's better it you start your own new thread using the [+Post New Thread] button top left

Reference this one if it contains pertinent information

Aurelius M
05-25-2020, 12:17 AM
Thanks for the reply.
My problem is, I don't know to which topic (category) my post belongs.
It is very confusing for me. Maybe You can help me to choose which topic.
Marcus