Question Yet another Get text from HTML

G441

New member
Joined
Apr 2, 2013
Messages
4
Programming Experience
Beginner
I have tried everything (of course not) but I can´t solve this problem. I have a Webbrowser in my form and I send a question with submit to a website (that works). Then, of course, I need to find the answer when the website i updated. There I have some problem. The answer fom the website is something like this:
HTML:
</div>
<div class="title row">
<h1 class="fn n">
<span class="given-name">Anders</span>
<span class="family-name">Karlsson</span>
</h1>
</div>

What I need to get from above is "given-name", in this case "Anders" and "family-name", in this case "Karlsson". What code should I use to retreive this?

VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] WebBrowser1_DocumentCompleted([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] WebBrowserDocumentCompletedEventArgs) _[/SIZE]
[SIZE=2]
[/SIZE] 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] WebBrowser1.DocumentCompleted[/SIZE]
 
[SIZE=2] 
[/SIZE] 
 
[SIZE=2] 

[COLOR=green]    TextBox1.Text = WebBrowser1.Document.GetElementsByTagName("given-name").ToString[/COLOR][/SIZE] 
[SIZE=2] 
 
 
[/SIZE] 
 
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/COLOR][/SIZE]

(As you see I also have some problem writing on this forum) Any ideas?
 
Hi,

You need to get the elements with the Tag name of "span" and then grad the inner text of that element where the Class attribute is equal to whatever condition you need to test. When testing a "class" attribute you need to use the term "className". i.e:-

VB.NET:
For Each current_Element As HtmlElement In myWebBrowser.Document.GetElementsByTagName("span")
  If current_Element.GetAttribute("className") = "given-name" Then
   MsgBox(current_Element.InnerText)
  End If
Next

Hope that helps.

Cheers,

Ian
 
Yes Ian, that made the trick. Thought i´ve tried every combination but obviusly not this one. Thank you and thank you again :semi-twins:
 
Just another thing.
My first question was to find the name of a person and that works fine now.
But I discovered that sometimes the answer is a company name and then the HTML-code is different, like this:

HTML:
<span class="num">1.</span> <h2><a class="fn org addax addax-cs_hl_hit_company_name_click" href="/f/sportmanship-invest-ab:8118342" title="Mer information om Sportmanship Invest AB">Sportmanship Invest AB</a></h2> </div>

where the information I need is "Sportmanship Invest AB".
I don´t know in advance if it is a private name or a company name so I need to cover both.
In the company case could it be possible to get the whole row to a string, then I can get the company name by splitting the string?

VB.NET:
If current_Element.GetAttribute("className") = "num" Then
                    'get the whole row, maybe?
End If

I guess there i a better way ...
 
Hi,

I am surprised you could not get this. The inner text of a "span" Tag with a Class name of "num" is "1.". You have used the right principle, just not the right information. You should now be getting the InnerText of an "a" Tag with a Class name that begins with "fn org".

Hope that helps.

Cheers,

Ian
 
You are right again. I'm useless when it comes to HTML. I did?nt see that there was a new Tag, I thought the whole row was the same Tag.
Well, it works. Thank you!
 
Back
Top