PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: Robert Rioja on May 19, 2016, 09:37:49 PM

Title: How to get web site source
Post by: Robert Rioja on May 19, 2016, 09:37:49 PM
I am hoping that someone can help me.  How can I read the source of a web site page into a string?  With a known URL, I want to log on to it and then download the source of that page into a string or array of strings.
Thanks,
Robert
Title: Re: How to get web site source
Post by: Eddy Van Esch on May 20, 2016, 05:20:02 AM
Here is what I use.
There is lots of error-handling code in it that is commented out. I decided to leave it in for you to see.

Call the function like this:

   %GetWebPage_TimeOut        = 20000  'ms

    sPage = "http://www.google.com/ig/api?stock=AAPL"
    sTxt = ""
    RetrieveWebPage "google.com", sPage, %GetWebPage_TimeOut, sTxt



'----------------------------------------------------------
FUNCTION RetrieveWebPage(sSite AS STRING, sPage AS STRING, lTimeOut AS LONG, sTxt AS STRING) AS LONG
    'Retrieve data from a web page.
    'INPUT : sSite    : website to retrieve data from
    '        sPage    : web page to retrieve
    '        lTimeOut : Time to wait for an answer [ms]
    'OUTPUT: sTxt     : Text retrieved from web page           
    'RETURN: 0 : no error
    '        %NEW_CouldNotConnect2Site : error
   
    LOCAL ff AS LONG
    LOCAL sBuffer, sMess AS STRING
   
    ON ERROR GOTO ErrorHandler
   
    sTxt = ""   
        ' Connecting...
    ff = FREEFILE   
    'Tcp Open Port 80 At sSite As #ff Timeout lTimeOut
    TCP OPEN "http"  AT sSite AS #ff TIMEOUT lTimeOut

        ' Could we connect to site?
'    If Err Then
'        Function = %NEW_CouldNotConnect2Site
'        sMess = "Error" + Str$(ErrClear) + " connecting to website: " + sSite
'        Tcp Close #ff
'        ShowMessage(sMess)
'        LogToFile(sMess)
'        Exit Function
'    End If
   
        ' Send the GET request...
    TCP PRINT #ff, "GET " + sPage + " HTTP/1.0"
'        If Err Then
'            Function = %NEW_CouldNotConnect2Site
'            sMess = "Error" + Str$(ErrClear) + " connecting to website: " + sSite
'            Tcp Close #ff
'            ShowMessage(sMess)
'            LogToFile(sMess)
'            Exit Function
'        End If   
   
    TCP PRINT #ff, ""
'        If Err Then
'            Function = %NEW_CouldNotConnect2Site
'            sMess = "Error" + Str$(ErrClear) + " connecting to website: " + sSite
'            Tcp Close #ff
'            ShowMessage(sMess)
'            LogToFile(sMess)
'            Exit Function
'        End If 
       
           
        ' Retrieve the page...
    DO
        TCP RECV #ff, 4096, sBuffer
'            If Err Then
'                Function = %NEW_CouldNotConnect2Site
'                sMess = "Error" + Str$(ErrClear) + " connecting to website: " + sSite
'                Tcp Close #ff
'                ShowMessage(sMess)
'                LogToFile(sMess)
'                Exit Function
'            End If         
        sTxt = sTxt + sBuffer
    LOOP WHILE ISTRUE(LEN(sBuffer)) AND ISFALSE(ERR)
   
    REPLACE $CRLF WITH $CR IN sTxt
    '  REPLACE $TAB WITH "" IN sTxt
    ' 
    '  WHILE INSTR(Entire_page, " ") > 0
    '  REPLACE " " WITH "" IN sTxt
    '  WEND
   
    '    ff = FreeFile
    '    Open "output.txt" For Output As #ff
    '    Print #ff, sTxt
    '    Close #ff
   
    'MsgBox Left$(sTxt, 6000)

        ' Close the TCP/IP port...
    TCP CLOSE #ff
    FUNCTION = 0

EXIT FUNCTION

ErrorHandler:
    FUNCTION = %NEW_CouldNotConnect2Site
    sMess = "Error" + STR$(ERRCLEAR) + " connecting to website: " + sSite
    TCP CLOSE #ff
    ShowMessage(sMess)
    LogToFile(sMess)
EXIT FUNCTION
   
END FUNCTION
Title: Re: How to get web site source
Post by: Petrus Vorster on May 20, 2016, 05:55:56 AM
I have been looking for something in this line a long time.

I need to send a Item tracking number to a public website and retrieve the response back.
Would something like that be possible?
Title: Re: How to get web site source
Post by: Eddy Van Esch on May 20, 2016, 06:03:31 AM
Quote from: Petrus Vorster on May 20, 2016, 05:55:56 AM
I need to send a Item tracking number to a public website and retrieve the response back.
Would something like that be possible?
Getting the response should be possible using my function below.
You only need to figure out how to send the number to the website. I assume the number is normally entered on the website form 'by hand' and then sent by hitting ENTER or clicking a SEND button?

Title: Re: How to get web site source
Post by: Petrus Vorster on May 20, 2016, 06:09:48 AM
100% correct.
https://www.postoffice.co.za/ContactUs/trackandtracedomestic.html (https://www.postoffice.co.za/ContactUs/trackandtracedomestic.html)

They wont budge on giving access to anyone. Understandable...
The send part I cant figure out either.
The plan is for my app to do multiple trackings at night on quiet networks, end have the responses ready the next day or do follow-ups for me.
Title: Re: How to get web site source
Post by: Robert Rioja on May 20, 2016, 09:44:18 PM
Hi Eddy,
Thank you very much for your help.
Robert
Title: Re: How to get web site source
Post by: Chris Maher on May 21, 2016, 02:04:12 PM
Petrus,

Good to see you are on with the tracking part now!

I think you probably need to start looking here in this Iframe (that is actually embedded in that page) for your information..

https://tracking.postoffice.co.za/TrackNTrace/TrackNTrace.aspx?id=12345678 (https://tracking.postoffice.co.za/TrackNTrace/TrackNTrace.aspx?id=12345678)

If you add your tracking barcode number in place of the "12345678" in the above you may find that it gives you the page of tracking information that you need to download and then parse.

Chris.
Title: Re: How to get web site source
Post by: Petrus Vorster on May 22, 2016, 03:18:15 PM
Hey Chris!

You know your stuff around these tracking issues and barcoding!
Thanks a million, its so simple.
I have no Web programming knowledge but i get the feeling i must sharpen up quickly.

Thanks a million, this works pretty cool!
Title: Re: How to get web site source
Post by: Petrus Vorster on May 22, 2016, 03:38:34 PM
Ok the string works like a charm, but the example above doesnt get feedback.
But then again i am so new at this kind of thing, i would not know where to look.

For a test i have just added a multine txtbox to see if i could get a return, using some of the code above.
Nothing yet.
Will read some more on this topic, but if you know a simple solution I will be grateful!

Thanks for all the help!