Results 1 to 8 of 8

Thread: Finding certain part of text and extracting.

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2012
    Posts
    51

    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*

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2012
    Posts
    51

    Re: Finding certain part of text and extracting.

    Quote Originally Posted by jmcilhinney View Post
    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!

  4. #4
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    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:
    1. Private Sub GetMetaDescription()
    2.     If (WebBrowser1.Document IsNot Nothing) Then
    3.         Dim Elems As HtmlElementCollection
    4.         Elems = WebBrowser1.Document.GetElementsByTagName("meta")
    5.  
    6.         For Each elem As HtmlElement In Elems
    7.             Dim NameStr As String = elem.GetAttribute("content")
    8.  
    9.             If ((NameStr IsNot Nothing) And (NameStr.Length <> 0)) Then
    10.                 If NameStr.ToLower().Equals("abcde") Then
    11.                     MessageBox.Show(NameStr)
    12.                 End If
    13.             End If
    14.         Next
    15.     End If
    16. End Sub

    Other than that such as using HttpWebRequest class, you can use HtmlAgilityPack to navigate the page source elements.

    - kgc
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2012
    Posts
    51

    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.
    Last edited by jj103; Sep 6th, 2017 at 10:48 AM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2012
    Posts
    51

    Re: Finding certain part of text and extracting.

    Quote Originally Posted by jmcilhinney View Post
    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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Finding certain part of text and extracting.

    These lines:
    vb.net Code:
    1. Dim NameStr As String = elem.GetAttribute("content")
    2. *
    3. If ((NameStr IsNot Nothing) And (NameStr.Length <> 0)) Then
    4.     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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width