Results 1 to 9 of 9

Thread: Retrieving data from a webpage

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    13

    Retrieving data from a webpage

    Hi

    Im a beginner to Vb.net .I would like to know how to get data from a webpage. Am I supposed to use ASP.Net?


    Thanks

    Matt

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

    Re: Retrieving data from a webpage

    You'll need to give more information. Do you want to display a Web page in a browser window or in your own application? Do you want to get some data from a page that is already displayed in a browser? Do you want to get the source for a page from a Web server without displying the rendered page?
    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
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Retrieving data from a webpage

    i am after doing the same sort of thing. i have posted a topic on it. i want to get data from a webpage and store it into a database or text file maybe? i want to get the titles of applications from a website, www.pspupdates.com in the files section. is it possible to only extract the titles of the applications and save them into a text file?

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Retrieving data from a webpage

    mimminito, heres a quick example i wrote that retrieves the application titles from that website (In VB 2005):

    (add a listbox called listbox1 to your form)
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         If IO.File.Exists(Application.StartupPath & "\heh.txt") Then
    3.             IO.File.Delete(Application.StartupPath & "\heh.txt")
    4.         End If
    5.         My.Computer.Network.DownloadFile("http://dl.qj.net/Gaming-Consoles-PSP/catid/106", Application.StartupPath & "\heh.txt")
    6.         Dim sr As New IO.StreamReader(Application.StartupPath & "\heh.txt")
    7.         Dim strLines() As String = Strings.Split(sr.ReadToEnd, Environment.NewLine)
    8.         sr.Close()
    9.         IO.File.Delete(Application.StartupPath & "\heh.txt")
    10.         Dim temp1 As Integer
    11.         Dim temp2 As Integer
    12.         For Each str As String In strLines
    13.             str = Trim(str)
    14.             If str.StartsWith("<td height=" & Chr(34) & "15" & Chr(34) & "><a href") Then
    15.                 temp1 = str.IndexOf("class=" & Chr(34) & "filename" & Chr(34) & ">") + ("class=" & Chr(34) & "filename" & Chr(34) & ">").Length
    16.                 temp2 = str.IndexOf("</a>") - (str.IndexOf("class=" & Chr(34) & "filename" & Chr(34) & ">") + ("class=" & Chr(34) & "filename" & Chr(34) & ">").Length)
    17.                 ListBox1.Items.Add(str.Substring(temp1, temp2))
    18.             End If
    19.         Next
    20.     End Sub
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Retrieving data from a webpage

    Cheers Atheist. I have got Visual Basic 2005 Express Edition and Visual Studio 2005. When i try the code in Visual Basic 2005 Express Edition i get an error, 'Statement is not valid in namespace' Any suggestions? Thanks for the code though i think this will work. I am fairly new to this type of coding, so i may sound a little stupid sorry

  6. #6
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Retrieving data from a webpage

    I have got it to work thankyou very much. I will reply soon with my results

  7. #7
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Retrieving data from a webpage

    Atheist, i was wondering if you could explain your coding a little more for me. I would like to understand it a little more, then i should be able to work on it myself. Thanks if you can

  8. #8
    New Member
    Join Date
    May 2006
    Posts
    10

    Re: Retrieving data from a webpage

    i got a lil problem now. i can make out most of your coding now, (apart from why a text file is created and then deleted) and i am able to change what i need. but when i go to say the development section on the webpage, the source code is diff to the other page. there is no 'class filename' only a href, so how can i go about getting the applications name?

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Retrieving data from a webpage

    Hello again
    First the page is downloaded as a textfile, then its read into a string. Since we have all the text in the string we dont need the textfile anymore, thats why its deleted (And if its not deleted the code will generate an error the next time its run)

    The development page...

    First of we begin with the basics:
    VB Code:
    1. If IO.File.Exists(Application.StartupPath & "\heh.txt") Then
    2.             IO.File.Delete(Application.StartupPath & "\heh.txt")
    3.         End If
    4.         My.Computer.Network.DownloadFile("http://dl.qj.net/Gaming-Consoles-PSP/catid/106", Application.StartupPath & "\heh.txt")
    5.         Dim sr As New IO.StreamReader(Application.StartupPath & "\heh.txt")
    6.         Dim strLines() As String = Strings.Split(sr.ReadToEnd, Environment.NewLine)
    7.         sr.Close()
    8.         IO.File.Delete(Application.StartupPath & "\heh.txt")


    Then we continue with this code:

    VB Code:
    1. Dim blnFound As Boolean = False
    2.         Dim intTemp_1 As Integer
    3.         Dim i As Integer
    4.         Dim q As Integer
    5.         For Each str As String In strLines
    6.             If blnFound = False Then 'If it has not found the application table
    7.                 If Trim(str) = "<table width=" & Chr(34) & "476" & Chr(34) & " class=" & Chr(34) & "contentcell" & Chr(34) & ">" Then
    8.                     blnFound = True 'Application table found!
    9.                 End If
    10.             Else 'If it has found the application table
    11.                 If Trim(str).StartsWith("<td><a href") Then 'If the line starts with <td><a href
    12.                     intTemp_1 = 0
    13.                     For i = 0 To str.Length 'Loop through the line and stop on the second >
    14.                         If str.Substring(i, 1) = ">" Then
    15.                             intTemp_1 = intTemp_1 + 1
    16.                         End If
    17.                         If intTemp_1 = 2 Then
    18.                             Exit For
    19.                         End If
    20.                     Next
    21.                     For q = i To str.Length ' Loop through the line (starting on the position of the second >, to find the first <
    22.                         If str.Substring(q, 1) = "<" Then
    23.                             Exit For
    24.                         End If
    25.                     Next
    26.                     If str.Substring((i + 1), (q - i) - 1) = "Users questions" Then
    27.                         Exit For ' Exit if we have come to User questions (we dont want that do we)
    28.                     End If
    29.                     If Not str.Substring((i + 1), (q - i) - 1) = "" Then
    30.                         ListBox1.Items.Add(str.Substring((i + 1), (q - i) - 1))
    31.                     End If
    32.                 End If
    33.                 End If
    34.         Next


    There! Just ask if you wonder something
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

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