Results 1 to 15 of 15

Thread: Read text from a website

  1. #1
    Addicted Member
    Join Date
    Jun 12
    Posts
    152

    Read text from a website

    Hello,
    I'd like someone to give me the code for reading text of a website if thats possible.
    For example
    I want to read this from a website
    <p class="welcome-msg">Welcome, username</p>
    So the program will find the welcome-msg class
    and will write down
    Welcome, Username

  2. #2
    Addicted Member
    Join Date
    Jun 12
    Posts
    152

    Re: Read text from a website

    bumb

  3. #3
    Frenzied Member
    Join Date
    Nov 05
    Posts
    1,808

    Re: Read text from a website

    Try this.

    vb.net Code:
    1. Dim html As String = "<p class=""welcome-msg"">Welcome, username</p>"
    2.  
    3. Debug.Print(Regex.Match(html, "(?<=<p class=""welcome-msg"">).+(?=</p>)").Value)

  4. #4
    Addicted Member
    Join Date
    Jun 12
    Posts
    152

    Re: Read text from a website

    Quote Originally Posted by Chris001 View Post
    Try this.

    vb.net Code:
    1. Dim html As String = "<p class=""welcome-msg"">Welcome, username</p>"
    2.  
    3. Debug.Print(Regex.Match(html, "(?<=<p class=""welcome-msg"">).+(?=</p>)").Value)
    What is Regex?

  5. #5
    Frenzied Member
    Join Date
    Nov 05
    Posts
    1,808

    Re: Read text from a website

    I'll give you the same reply I gave somebody else 10 minutes ago.

    Hover your mouse cursor over the red underscore, click on the red exclamation mark that appears and click on the 'Import' line.

  6. #6
    Addicted Member
    Join Date
    Jun 12
    Posts
    152

    Re: Read text from a website

    Quote Originally Posted by Chris001 View Post
    I'll give you the same reply I gave somebody else 10 minutes ago.

    Hover your mouse cursor over the red underscore, click on the red exclamation mark that appears and click on the 'Import' line.
    And how can i make it so it will appear the welcome message on a textbox?

  7. #7
    Frenzied Member
    Join Date
    Nov 05
    Posts
    1,808

    Re: Read text from a website

    vb.net Code:
    1. TextBox1.Text = Regex.Match(html, "(?<=<p class=""welcome-msg"">).+(?=</p>)").Value

  8. #8
    Addicted Member
    Join Date
    Jun 12
    Posts
    152

    Re: Read text from a website

    Quote Originally Posted by Chris001 View Post
    vb.net Code:
    1. TextBox1.Text = Regex.Match(html, "(?<=<p class=""welcome-msg"">).+(?=</p>)").Value
    For somereason textbox1.text writes Welcome, username. Where username i want to write the username it appears on the website

  9. #9
    Frenzied Member
    Join Date
    Nov 05
    Posts
    1,808

    Re: Read text from a website

    I simply showed you how to extract the text between "<p class="welcome-msg">" and "</p>".

    If you want to extract it from a webpage, then you must first download the webpage. You can do that with WebClient DownloadString.

    http://msdn.microsoft.com/en-us/library/fhd1f0sw

  10. #10
    Addicted Member
    Join Date
    Jun 12
    Posts
    152

    Re: Read text from a website

    Quote Originally Posted by Chris001 View Post
    I simply showed you how to extract the text between "<p class="welcome-msg">" and "</p>".

    If you want to extract it from a webpage, then you must first download the webpage. You can do that with WebClient DownloadString.

    http://msdn.microsoft.com/en-us/library/fhd1f0sw
    I am sorry but i never understand msdn.Microsoft.com, i will try though. But if it is possible for you, give me the full code. so i can read it and understand what it is from the source. Thanks

  11. #11
    Frenzied Member
    Join Date
    Nov 05
    Posts
    1,808

    Re: Read text from a website

    I bet you haven't even looked at that link. It's only three lines of code to download a webpage.

    vb.net Code:
    1. Imports System.Text.RegularExpressions
    2. Imports System.Net
    3.  
    4. Dim html As String
    5.  
    6. Using wc As New WebClient
    7.     html = wc.DownloadString("http://website.com")
    8. End Using
    9.  
    10. TextBox1.Text = Regex.Match(html, "(?<=<p class=""welcome-msg"">).+(?=</p>)").Value

  12. #12
    Addicted Member
    Join Date
    Jun 12
    Posts
    152

    Re: Read text from a website

    Quote Originally Posted by Chris001 View Post
    I bet you haven't even looked at that link. It's only three lines of code to download a webpage.

    vb.net Code:
    1. Imports System.Text.RegularExpressions
    2. Imports System.Net
    3.  
    4. Dim html As String
    5.  
    6. Using wc As New WebClient
    7.     html = wc.DownloadString("http://website.com")
    8. End Using
    9.  
    10. TextBox1.Text = Regex.Match(html, "(?<=<p class=""welcome-msg"">).+(?=</p>)").Value
    Well you are wrong. I looked at the link. I was realy close! I am proud since it was the first time i almost understand microsofts tutorials. I just didn't added the Imports System.Text.regularExpression . Anyways, thanks. I will try the code

    EDIT: Wrong, they automaticly got in, so i don't know why my code didn't worked last time.

  13. #13
    Addicted Member
    Join Date
    Jun 12
    Posts
    152

    Re: Read text from a website

    Quote Originally Posted by Chris001 View Post
    I bet you haven't even looked at that link. It's only three lines of code to download a webpage.

    vb.net Code:
    1. Imports System.Text.RegularExpressions
    2. Imports System.Net
    3.  
    4. Dim html As String
    5.  
    6. Using wc As New WebClient
    7.     html = wc.DownloadString("http://website.com")
    8. End Using
    9.  
    10. TextBox1.Text = Regex.Match(html, "(?<=<p class=""welcome-msg"">).+(?=</p>)").Value
    Here is another problem, Although the code works perfect, after login, it says Welcome guest, and not welcome name. And i can't findout why it does that. any idea?

  14. #14
    Junior Member PowerProg's Avatar
    Join Date
    Jan 12
    Posts
    17

    Arrow Re: Read text from a website

    Code:
    Imports System.Text.RegularExpressions
    Imports System.Net
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Html As String
            Dim wc As New WebClient
            Html = wc.DownloadString("http://localhost/index1.html")
    
            LinkLabel1.Text = Regex.Match(Html, "(?<=<a class=""Url"">).+(?=</a>)").Value
        End Sub
    End Class
    This is Correct solution but
    how to read URL ??

  15. #15
    Addicted Member
    Join Date
    Jun 12
    Posts
    152

    Re: Read text from a website

    For some reason my website doesn't get updated really quick.
    So as result, i don't get a Welcome, username and i get a welcome, Guest.
    But if i see the webbrowser, i can see that it says on the browser welcome, username
    and it doesn't say welcome, Guest
    Any fixes?
    Thanks!

Posting Permissions

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