Finding certain part of text and extracting.
Hi. It's been many years since programming but I've had an idea for long but never knew how do ask. (I was very young at the time.)
Basically, I need a way to search through text and find a part of the text. Let's say it looks like this:
(The text is from a page source)
Code:
<meta property="example:content1" content="123456" />
<meta property="example:content2" content="abcde" />
<meta property="example:content3" content="fghijkl />
<meta property="example:content4" content="mnopq" />
And all I want is the "adcde" put in textbox1 from "example:content2"
I know how to get page source so know I don't need that information. I just need to know how to get the code to look through the text and get "adcde"
Any help would be awesome guys, thank you so much for reading
*Know that I am self-taught, sorry if this is rather easy*
Re: Finding certain part of text and extracting.
So, you want to get the value of the 'content' attribute from the 'meta' element with a 'property' attribute of "example:content2", correct?
Re: Finding certain part of text and extracting.
Quote:
Originally Posted by
jmcilhinney
So, you want to get the value of the 'content' attribute from the 'meta' element with a 'property' attribute of "example:content2", correct?
I think so!
Re: Finding certain part of text and extracting.
If your using the webbrowser control, you can loop through the meta elements using HtmlElementCollection class.
VB.NET Code:
Private Sub GetMetaDescription()
If (WebBrowser1.Document IsNot Nothing) Then
Dim Elems As HtmlElementCollection
Elems = WebBrowser1.Document.GetElementsByTagName("meta")
For Each elem As HtmlElement In Elems
Dim NameStr As String = elem.GetAttribute("content")
If ((NameStr IsNot Nothing) And (NameStr.Length <> 0)) Then
If NameStr.ToLower().Equals("abcde") Then
MessageBox.Show(NameStr)
End If
End If
Next
End If
End Sub
Other than that such as using HttpWebRequest class, you can use HtmlAgilityPack to navigate the page source elements.
- kgc
Re: Finding certain part of text and extracting.
Well I'm not looking for attribute (I believe, the adbcde in the example) in particular. I need it to extract the content from example:content2.
Re: Finding certain part of text and extracting.
You've already been given everything you need. You don't have to wait for someone else to write the exact you code for you. You're allowed to use the principles that have already been demonstrated to write final code for yourself. Post #4 demonstrates how to access every 'meta' tag, how to get the value of an attribute and how to test whether that value is equal to something specific. What more do you need? Get the 'property' attribute value, check whether it's the one you need and, if it is, get the 'content' attribute value. Whenever someone provides you with information, think about how you can use that information to resolve your issue, in part or in full, for yourself.
Re: Finding certain part of text and extracting.
Quote:
Originally Posted by
jmcilhinney
You've already been given everything you need. You don't have to wait for someone else to write the exact you code for you. You're allowed to use the principles that have already been demonstrated to write final code for yourself. Post #4 demonstrates how to access every 'meta' tag, how to get the value of an attribute and how to test whether that value is equal to something specific. What more do you need? Get the 'property' attribute value, check whether it's the one you need and, if it is, get the 'content' attribute value. Whenever someone provides you with information, think about how you can use that information to resolve your issue, in part or in full, for yourself.
I'm sorry. I read it as if looking for a partifuclar attribute. Didn't know how to use the information given. I messed around with it and couldn't figure it out. Thought I miscommunicated but I misread. Clearly coding still isn't for me.
Re: Finding certain part of text and extracting.
These lines:
vb.net Code:
Dim NameStr As String = elem.GetAttribute("content")
*
If ((NameStr IsNot Nothing) And (NameStr.Length <> 0)) Then
If NameStr.ToLower().Equals("abcde") Then
get the value of the 'content' attribute and check whether it is equal to a "abcde". What do you want to do? You want to get the value of the 'property' attribute and check whether it is equal to a "example:content2". How do you think you would change that code to do what you want? Once you've done that, you then want to get the value of the 'content' attribute. The code already shows how to do exactly that so there's nothing to even think about for that.