HelpHow to separate individual HTML elements

Posts 1–12 of 12 · Page 1 of 1
How to separate individual HTML elements
Hey all.
I have a project im working on which uses some features of online websites that don't have an api.
A bit of a way through the code an element returns four linking statements with three of them having the property style="display:none".
What i want to do is just get the html link in the <a> statement without the display none attribute.
I've spent close to 2 hours on it and have even tried using an xml reader to try to get the info but no luck.

Code:
<DIV id=example style="DISPLAY: block"><A style="DISPLAY: none" href="fakelink">Fake</A>
<A style="DISPLAY: none" href="fakelink">Fake</A>
<A href="real link">Real</A>
<A style="DISPLAY: none" href="fakelink">Fake</A>
<A style="DISPLAY: none" href="fakelink">Fake</A></DIV>
I wouldnt be able to separate them by the actual link either as it changes depending on the users input.
Not sure if the real link has an assigned style or if it's just "display:normal" or etc.

Any help would be greatly appreciated
+bump, played around with a fair bit of stuff but no luck.
I don't exactly follow, do you want the value of href? "fakelink" stored as an object?
Quote Originally Posted by Sammy View Post
I don't exactly follow, do you want the value of href? "fakelink" stored as an object?
I'd just like to get the entire <a> </a> element with the real link.
They're not separated by a new line or anything either.
"an element returns four linking statements"

is that returned value a string? do you want to cut out the parts between <a> and </a>?

I'm sorry but your intentions aren't very clear to me
Quote Originally Posted by Sammy View Post
"an element returns four linking statements"

is that returned value a string? do you want to cut out the parts between <a> and </a>?

I'm sorry but your intentions aren't very clear to me
no, i'd like to cut out all the <a>'s that are fake links. and only have the real one left.
So you still did not answer if what you have are strings but lets assume you do.

sooo.... in code:

Code:
Dim TestString As String = "<DIV id=example style=\"DISPLAY: block\"><A style=\"DISPLAY: none\" href=\"fakelink">Fake</A>
<A style=\"DISPLAY: none\" href=\"fakelink\">Fake</A>
<A href=\"real link\">Real</A>
<A style=\"DISPLAY: none\" href=\"fakelink\">Fake</A>
<A style=\"DISPLAY: none\" href=\"fakelink\">Fake</A></DIV>"

Dim aString As String = Replace(TestString, "<A", "")
Dim bString As String = Replace(aString, "</A>", "")
Now bString will be without the A tags...

if you want display none removed

Code:
Dim aString As String = Replace(TestString, "style=\"DISPLAY: none\"", "")
Quote Originally Posted by Sammy View Post
So you still did not answer if what you have are strings but lets assume you do.

sooo.... in code:

Code:
Dim TestString As String = "<DIV id=example style=\"DISPLAY: block\"><A style=\"DISPLAY: none\" href=\"fakelink">Fake</A>
<A style=\"DISPLAY: none\" href=\"fakelink\">Fake</A>
<A href=\"real link\">Real</A>
<A style=\"DISPLAY: none\" href=\"fakelink\">Fake</A>
<A style=\"DISPLAY: none\" href=\"fakelink\">Fake</A></DIV>"

Dim aString As String = Replace(TestString, "<A", "")
Dim bString As String = Replace(aString, "</A>", "")
Now bString will be without the A tags...

if you want display none removed

Code:
Dim aString As String = Replace(TestString, "style=\"DISPLAY: none\"", "")
no, i want to return only the real link. that gives me all the fake links as well.
Quote Originally Posted by Biesi View Post
Depending on how complex the HTML is you could just use a simple REGEX (for better html parsing I'd suggest the Html Agility Pack (HAP))

e.g.
Code:
(?i)<a href=\"(.*)\">.*<\/a>
https://regex101.com/r/tQ8wA6/1
Checked out the html agility pack and not much documentation on it :/
The regex was great though. Cheers man
oooh, i still don't know if it returns every string apart, say it doesnt

you could do something like this:

Code:
Dim s As String = "<DIV id=example style=\"DISPLAY: block\"><A style=\"DISPLAY: none\" href=\"fakelink">Fake</A>
<A style=\"DISPLAY: none\" href=\"fakelink\">Fake</A>
<A href=\"real link\">Real</A>
<A style=\"DISPLAY: none\" href=\"fakelink\">Fake</A>
<A style=\"DISPLAY: none\" href=\"fakelink\">Fake</A></DIV>"

 Dim searchString As String = "<A href="
 Dim startindex As Integer = s.IndexOf(searchString)
 searchString = "</A>" + searchString.Substring(1)
 Dim endIndex As Integer = s.IndexOf(searchString)
 Dim substring As String = s.Substring(startIndex, endIndex + searchString.Length - StartIndex)
I honestly don't know visual basic that well and this is just from reading from MSDN, which you should do too. But this might help you. RegEx is a good option too!
RichTextBox1.Text is basically where you put your HTML source.

Code:
Dim htmlCode As String = RichTextBox1.Text
Dim splittedText() = Split(htmlCode, """")
Dim splittedText2() = Split(splittedText(12), ">")
Dim link As String = splittedText2(1).Replace("</A", "")
Hope this helps. Fiddle with the position if you want to get other values.
Demo: http://i.imgur.com/8pPE233.gif
Posts 1–12 of 12 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us