Homework question

Post

Posted
Rating:
#1 (In Topic #980)
Avatar
Trainee
Im trying to build a horoscope app for school which reads the data from an URL. The question is: How can I read contents of a URL page and get only the data I want? Like the string I want is after "<meta property="og:description" content=xxxxxxxxxxx"" .
I'm not interested in being spoon feed but I really need some help!
Thanks :P
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
Welcome to the forum.

Option 1 would be to sign up for an API, that way you could easily get just the data you want. Have a look here
Option 2. As far as I can see, you can read all the page, not part of it. However, once you have the page, you can use Gambas to find the part you want.  

Here is some code that will download the whole page of a website and save it in your program's folder. You can then search the file for the text position and grab what you need.

Does that help? No spoon-feeding intended! :D

Code (gambas)

  1. ''NEEDS gb.net.curl
  2.  
  3. Public Sub Form_Open()
  4.  
  5.   Dim sPage As String
  6.  
  7.   sPage = GetHScope("https://www.elle.com/horoscopes/daily/a98/taurus-daily-horoscope/")
  8.  
  9.   File.Save(Application.Path &/ "hScope.txt", sPage)
  10.  
  11.  
  12. Public Sub GetHScope(sURL As String) As String
  13.  
  14.   Dim hClient As HttpClient                                                     'To create a HTTP Client
  15.   Dim sResult As String                                                         'To store the word's meaning
  16.  
  17.   hClient = New HttpClient As "hClient"                                         'Create a HTTP Client
  18.   With hClient                                                                  'With the Client..
  19.     .URL = sURL                                                                 'Set up the URL
  20.     .Async = False                                                              'No Asynchronous transmission?
  21.     .TimeOut = 60                                                               'Don't hang around waiting for more than 60 seconds
  22.     .get                                                                        'Get the data
  23.  
  24.   If Lof(hClient) Then sResult = Read #hClient, Lof(hClient)                    'When all the data is downloaded store it in sResult
  25.   Return sResult
  26.  
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Trainee
It does help! Do you mind if I ask you how can I get the string I want? : :D
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 Its returned by that function, …

in Main()
sPage =  …

Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Trainee
 Yes, but it also returns "garbage" which I don't use
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’

twbro54l said

It does help! Do you mind if I ask you how can I get the string I want? : :D

If you mean how to get the correct part out of the saved data then let me know which site you are wanting to use.
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Trainee
Here's the site! Sorry!
Horoscop Berbec 09.05.2016
I can get them in english too, but I will be reading the data based on the date they want. Like for example yesterday, one week ago or one month ago.
Here's what I could get from the source of the page, where the text is located at.

Code

<div class="zodie-content-texts black">
                           <p>Dimineata promite a fii linistita. Interactioneaza cu cei care iti plac si nu incerca sa iei noi initiative. Ofertele minore se pot dovedi de succes. O serie de sarcini materiale actuale pot fi realizate cu succes la fel de bine.</p>
                </div>
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’

twbro54l said

Im trying to build a horoscope app for school which reads the data from an URL. The question is: How can I read contents of a URL page and get only the data I want? Like the string I want is after "<meta property="og:description" content=xxxxxxxxxxx"" .
I'm not interested in being spoon feed but I really need some help!
Thanks :P

Please explain exactly what you need better. and what your experience with gambas is.

You say you do not want to be spoon-fed but it also sounds like you are very new to gambas and have no idea where to begin.
If that's the case then don't worry about the spoon-feeding ;)

you will want to use String functions/operators.  have you read the wiki?
http://gambaswiki.org/wiki/cat/string
http://gambaswiki.org/wiki/cat/stringop

Code (gambas)

  1. Dim aLines As String[]
  2. Dim iCount As Integer
  3.  
  4. aLines = Split(sPage, "\n")  ' split the page into lines.
  5.  
  6. For iCount = 0 To aLines.Max
  7.  
  8. ' Use the Like keyword to check a string pattern
  9.  If aLines[iCount] Like "<meta property=\"og:description\" content=*" Then
  10.    Print "Found Line";; aLines[iCount]
  11.    Print "The next line is "; aLines[iCount + 1]
  12.    Break   ' exit the loop as we are finished
  13.  
  14.  
  15.  

If the meta line you're seeking is in the header you may be able to use the Headers array http://gambaswiki.org/wiki/comp/gb.net.curl/httpclient/headers
Edit: forget that i tried and the meta properties cannot be found in the headers

Or you could modify Cogiers method to do it as you read the page.  this will be faster as the whole page will not need to be downloaded just the first few lines..
(See the next message)
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’
This will extract the contents of the meta tag you are seeking.

Code (gambas)

  1.  
  2. Public Sub Form_Open()
  3.  
  4.   Dim hClient As HttpClient                                                     'To create a HTTP Client
  5.   Dim sResult As String                                                         'To store the word's meaning
  6.  
  7.   hClient = New HttpClient As "hClient"                                         'Create a HTTP Client
  8.   With hClient                                                                  'With the Client..
  9.     .URL = "https://www.horoscop.ro/rac/"                                                                 'Set up the URL
  10.     .Async = False                                                              'No Asynchronous transmission?
  11.     .TimeOut = 60                                                               'Don't hang around waiting for more than 60 seconds
  12.     .get                                                                        'Get the data
  13.  
  14.   While Lof(hClient)    ' Lof has a value until all the file has been downloaded
  15.  
  16.     sResult = hClient.ReadLine()  ' Read 1 single line
  17.  
  18.    If sResult Like "<meta property=\"og:description\" content=*" Then      ' Here the line is found
  19.      sResult = Mid(sResult, InStr(sResult, "content=") + 9)   ' trim out the left side
  20.      sResult = Mid(sResult, 1, RInStr(sResult, "\"") - 1)        ' trim out the right side
  21.     hClient.Close    ' close the stream    
  22.     Break   ' exit the loop
  23.   Wend
  24.  
  25. Print sResult
  26.  
  27.  
  28.  

Prints…
Un castig financiar ar putea veni ca rezultat al unei miscari pe care nimeni nu se astepta sa o faci. Este posibil ca toata lumea sa fie foarte mandra de tine - si la fel vei fi si tu. Acesta este doa
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Trainee
 Thank you so much! Yes, I'm new to Gambas. I'm going to add the born-date, month and current-date (or the date they want the data for) in a Form and print it in a text area.
Ill post updates when I'm done. Thank you guys so much!
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘Guru’

twbro54l said

Thank you so much! Yes, I'm new to Gambas. I'm going to add the born-date, month and current-date (or the date they want the data for) in a Form and print it in a text area.
Ill post updates when I'm done. Thank you guys so much!

Well welcome to the wonderful world of gambas basic.

Have a good read of the wiki, especially the String pages i gave the links to.
With commands like Instr() and Mid() you can find and extract any data you need.
Online now: No Back to the top

Post

Posted
Rating:
#12
Avatar
Guru
cogier is in the usergroup ‘Guru’
This was a bit of fun.

Here is a good start to your program: -

<IMG src="https://www.cogier.com/gambas/HScope.png"> </IMG>

Attachment
Online now: No Back to the top
1 guest and 0 members have just viewed this.