Results 1 to 30 of 30

Thread: Reading a web page......

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    Kingston, Ontario, Canada
    Posts
    74

    Reading a web page......

    Alright,

    I need a bit of help here. I need to read a file online (the address is http://www.angelfire.com/ca2/TJTurtle/textfile.txt and I want to read the "y" and store it. For 3 points and a Tim-Bit, how would I go about doing this?

    BONUS: For triple points and a snack-pack of the Tim-Bits, how would I read http://xml.imood.org/moods.cgi and only store the stuff between <count> and </count>?

    SUPER BONUS!!!! I'll bump the points to 50 and toss you 100 Tim-Bits if you can tell me how to store all of the stuff between each of the <base> and </base> tags too.

    Let me know!
    To err is human, to really mess up, you need a computer.

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    For 3 points and a Tim-Bit:

    Slap an Inet control on a form, and use this
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3. Text1.Text = Inet1.OpenURL("http://www.angelfire.com/ca2/TJTurtle/textfile.txt")
    4.  
    5. End Sub
    'm going for the triple points and a snack-pack of the Tim-Bits, but for 100 Tim-Bits, you must be joking!

  3. #3
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Right, where's my triple points and a snack-pack of the Tim-Bits?!
    VB Code:
    1. Dim strFile As String
    2. Dim strCount As String
    3. Dim intPos As Long
    4. Dim intPos1 As Long
    5.  
    6. strFile = Inet1.OpenURL("http://xml.imood.org/moods.cgi")
    7.  
    8. 'extract <count></count>
    9. intPos = InStr(1, strFile, "<count>", vbTextCompare)
    10.  
    11. If intPos > 0 Then
    12.     intPos1 = InStr(intPos + 7, strFile, "</count>", vbTextCompare)
    13.         If intPos1 > 0 Then
    14.             'extract info
    15.             strCount = Mid$(strFile, intPos + 7, intPos1 - (intPos + 7))
    16.         End If
    17. End If
    18.  
    19. Text1.Text = strCount

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    Looks like I win it all;
    VB Code:
    1. Dim M%,N%,strText$,CurBase$
    2. strText = Inet1.OpenURL("http://xml.imood.org/moods.cgi")
    3. Do While Inet1.StillExecuting
    4.   DoEvents
    5. Loop
    6.   startInt = 1
    7.   Do
    8.     M = InStr(startInt, strText, "<base>")
    9.     If M = 0 Then Exit Do
    10.     M = M + Len("<base>")
    11.     N = InStr(M, strText, "</base>")
    12.     CurBase$ = Mid$(strText, M, N - M)
    13.     Debug.Print CurBase$
    14.     startInt = M
    15.   Loop
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5
    DaoK
    Guest
    They have an API to do it without Inet control.

  6. #6
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Originally posted by DaoK
    They have an API to do it without Inet control.
    Care to grace us with the name of that API?

  7. #7
    DaoK
    Guest
    Mathew Gates gave me that code few month ago:

    VB Code:
    1. Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
    2. "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As _
    3. String, ByVal szFileName As String, ByVal dwReserved As Long, _
    4. ByVal lpfnCB As Long) As Long
    5.  
    6. Private Function DownloadFile(URL As String, _
    7.     LocalFilename As String) As Boolean
    8.     Dim lngRetVal As Long
    9.     lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
    10.     If lngRetVal = 0 Then DownloadFile = True
    11. End Function
    12.  
    13. Private Sub Command1_Click()
    14.     DownloadFile "http://www.mypage.com/txtfile.txt", "c:\txtfile.txt"
    15.  
    16.     Open "C:\txtfile.txt" For Input As #1
    17.         Text1.Text = Input(LOF(1), 1)
    18.     Close #1
    19. End Sub

  8. #8
    DaoK
    Guest
    I really like that api because you dont need the Inet OCX and it more stable ( that what Matthew told me ).

  9. #9
    DaoK
    Guest
    DO you know how to add a progress bar on that code ?

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    Kingston, Ontario, Canada
    Posts
    74
    Wow!

    I guess the Tim-Bits helped! Now, you non-Canadians, what ARE Tim-Bits?

    Oh, and you Canadians cann fill 'em in.
    Last edited by Jigabug; Oct 2nd, 2001 at 08:07 PM.
    To err is human, to really mess up, you need a computer.

  11. #11
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Originally posted by Jigabug
    Now, you non-Canadians, what ARE Tim-Bits?
    Haven't got a clue

  12. #12
    DaoK
    Guest
    canada have Tim Bits

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    Kingston, Ontario, Canada
    Posts
    74
    Of COURSE we do! We have them on every other corner almost!
    To err is human, to really mess up, you need a computer.

  14. #14
    DaoK
    Guest

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    Kingston, Ontario, Canada
    Posts
    74

    Post

    For some extra fun, how would I get an Image/Picture box to open an online picture?
    To err is human, to really mess up, you need a computer.

  16. #16
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    by

    a) using a webbrowser control instead
    b) saving the file to a local disk using inet or that API code and displaying it from there

  17. #17
    DaoK
    Guest
    VB Code:
    1. Private Declare Function URLDownloadToFile Lib "urlmon" _
    2. Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL _
    3. As String, ByVal szFileName As String, ByVal dwReserved As Long, _
    4. ByVal lpfnCB As Long) As Long
    5.  
    6. Private Function DownloadFile(URL As String, _
    7. LocalFilename As String) As Boolean
    8.     Dim lngRetVal As Long
    9.     lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
    10.     If lngRetVal = 0 Then DownloadFile = True
    11.  
    12. End Function
    13.  
    14.  
    15. Private Sub Command1_Click()
    16.  
    17.     Dim Success As Boolean
    18.     Success = DownloadFile("http://www.vbsquare.com/images/vbsquare.gif", "C:\vbsquare.gif")
    19.     If Success = True Then
    20.         Picture1.Picture = LoadPicture("C:\vbsquare.gif")
    21.     Else
    22.         Msgbox "Could not load image!", 16
    23.     End If
    24.  
    25. End Sub

  18. #18
    DaoK
    Guest
    Matthew Gates CopyRight for that code two.

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    Kingston, Ontario, Canada
    Posts
    74
    Now, is there any way for it to be put RIGHT up against the top and left edges? It seems to like putting itself in a bit.
    To err is human, to really mess up, you need a computer.

  20. #20
    DaoK
    Guest
    I do not understand you ?

  21. #21
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Winsock Code...

    Originally posted by Jigabug
    For some extra fun, how would I get an Image/Picture box to open an online picture?
    Here's the fun. Using Winsock to download the file... Then load it into a picture box. See the code below. You will need to add a Picture Box control named Picture1 to a form, along with a Winsock control named Winsock1.
    VB Code:
    1. 'Coded by [Digital-X-Treme]
    2. Option Explicit
    3.  
    4. Private m_strWebPageData As String
    5.  
    6. Private Sub Form_Load()
    7.     'Establish connection.
    8.     Winsock1.Connect "vbforums.com", 80
    9. End Sub
    10.  
    11. Private Sub Winsock1_Close()
    12.  
    13.     Dim i As Long
    14.  
    15.     Debug.Print "Closed..."
    16.     Debug.Print "Transfer complete..."
    17.    
    18.     'Now you have the web page data, including the header.
    19.     'The header, if HTTP compliant, is trminated by the first blank line... vbCrlf & vbCrlf
    20.    
    21.     'Trim the header, if any, out.
    22.     i = InStr(1, m_strWebPageData, vbCrLf & vbCrLf)
    23.     If i <> 0 Then
    24.         m_strWebPageData = Mid(m_strWebPageData, i + 4)
    25.     End If
    26.  
    27.     'm_strWebPageData now contains the file contents.
    28.    
    29.     'Save it to file...
    30.     Open "C:\Temp.tmp_" For Binary As #1
    31.         Put #1, , m_strWebPageData
    32.     Close #1
    33.    
    34.     'Load picture from file...
    35.     Picture1.Picture = LoadPicture("C:\Temp.tmp_")
    36.  
    37. End Sub
    38.  
    39. Private Sub Winsock1_Connect()
    40.     Debug.Print "Connected..."
    41.     Debug.Print "Sending request..."
    42.    
    43.     'Include the header field "Connection: close"
    44.     'This indicates to HTTP/1.1 compliant servers that we want to close the connection
    45.     'after downloading this page...
    46.     'A trick that lets us know when the download is complete (winsock will close), so we
    47.     'don't need to parse the header fields and find a content-length field...
    48.    
    49.     Winsock1.SendData "GET /images/layout/top_left.gif HTTP/1.0" & vbCrLf & "Connection: close" & vbCrLf & vbCrLf
    50.  
    51.     'If the server you are using is HTTP/1.0 compliant, then you shouldn't send the 'connection'
    52.     'header field.
    53.     'You will need to parse the response header, and grab the content length field, then
    54.     'just keep recieveing data until the content has been downloaded...
    55.    
    56. End Sub
    57.  
    58. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    59.     Dim strData As String
    60.  
    61.     Winsock1.GetData strData, vbString
    62.     m_strWebPageData = m_strWebPageData & strData
    63.    
    64. End Sub

    Hope this helps
    Laterz
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  22. #22
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Originally posted by DaoK
    Mathew Gates gave me that code few month ago:

    VB Code:
    1. Private Declare Function URLDownloadToFile Lib "urlmon" Alias _
    2. "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As _
    3. String, ByVal szFileName As String, ByVal dwReserved As Long, _
    4. ByVal lpfnCB As Long) As Long
    5.  
    6. Private Function DownloadFile(URL As String, _
    7.     LocalFilename As String) As Boolean
    8.     Dim lngRetVal As Long
    9.     lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
    10.     If lngRetVal = 0 Then DownloadFile = True
    11. End Function
    12.  
    13. Private Sub Command1_Click()
    14.     DownloadFile "http://www.mypage.com/txtfile.txt", "c:\txtfile.txt"
    15.  
    16.     Open "C:\txtfile.txt" For Input As #1
    17.         Text1.Text = Input(LOF(1), 1)
    18.     Close #1
    19. End Sub
    Just a bit but wouldn't that code process for example a cgi script?

  23. #23
    DaoK
    Guest
    it's an api...i do not know about the cgi...

  24. #24

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    Kingston, Ontario, Canada
    Posts
    74
    Alright,

    Now, how would i read out EVERY item inside <base> and </base> and add them to a list box? The list is at http://xml.imood.org/moods.cgi

    I'm using the code posted by chrisjk, he wanted his triple points and the tim-bits.
    Last edited by Jigabug; Oct 11th, 2001 at 08:07 AM.
    To err is human, to really mess up, you need a computer.

  25. #25
    WALDO
    Guest

    For XML, you might want to look into...

    Try the microsoft XML parser. It can look across the internet for an XML source AND parse that document and extract all the <base></base> tags or whatever you want. It's a little easier than manually trying to parse the source after getting it using the Inet control or the WinInet API


  26. #26
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    Now, how would i read out EVERY item inside <base> and </base> and add them to a list box? The list is at http://xml.imood.org/moods.cgi
    Check the code I posted up above. Replace debug.print with MyList.Additem.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  27. #27
    WALDO
    Guest

    Something like this

    If you were using the oldest version of the XML parser:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim objXML As New MSXML.DOMDocument
    5.     Dim blnSuccess As Boolean
    6.    
    7.     objXML.async = False
    8.     blnSuccess = objXML.Load("http://xml.imood.org/moods.cgi")
    9.    
    10.     If Not blnSuccess Then
    11.         MsgBox _
    12.             "XML Error #" & objXML.parseError.errorCode & ": " & _
    13.             objXML.parseError.reason & vbCrLf & _
    14.             "Line " & objXML.parseError.Line & _
    15.             ", Position " & objXML.parseError.linepos & vbCrLf & _
    16.             objXML.parseError.srcText
    17.         Exit Sub
    18.     End If
    19.    
    20.     Dim xmlList As IXMLDOMNodeList
    21.     Dim x As IXMLDOMNode
    22.     Set xmlList = objXML.getElementsByTagName("imood/bundle/mood/base")
    23.    
    24.     For Each x In xmlList
    25.         Debug.Print x.Text
    26.         Me.Combo1.AddItem x.Text
    27.     Next
    28.    
    29. End Sub

    This code works for me!

  28. #28

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    Kingston, Ontario, Canada
    Posts
    74
    Alright, I'm now using the sunburnt code, any ideas why it would stop at "desperate"?
    To err is human, to really mess up, you need a computer.

  29. #29
    WALDO
    Guest

    All I have to say is...mine works perfectly.

    What is this gay ass imood crap, anyway?

  30. #30
    WALDO
    Guest

    For the count...

    Use my code above and change the parameter in getElementsByTagName to:

    "imood/bundle/count"

    You probably don't have to bother iterating (looping) through the results since you know there's only ever going to be one count.

    You can also reuse the same objXML object without going out and getting the same data again. the .xml property of that object stayed the same.

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