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
Homework question
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Homework question
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!
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!
''NEEDS gb.net.curl
Public Sub Form_Open()
Dim sPage As String
sPage = GetHScope("https://www.elle.com/horoscopes/daily/a98/taurus-daily-horoscope/")
File.Save(Application.Path &/ "hScope.txt", sPage)
End
Public Sub GetHScope(sURL As String) As String
Dim hClient As HttpClient 'To create a HTTP Client
Dim sResult As String 'To store the word's meaning
hClient = New HttpClient As "hClient" 'Create a HTTP Client
With hClient 'With the Client..
.URL = sURL 'Set up the URL
.Async = False 'No Asynchronous transmission?
.TimeOut = 60 'Don't hang around waiting for more than 60 seconds
.get 'Get the data
End With
If Lof(hClient) Then sResult = Read #hClient, Lof(hClient) 'When all the data is downloaded store it in sResult
Return sResult
End
Re: Homework question
It does help! Do you mind if I ask you how can I get the string I want? :
Re: Homework question
Its returned by that function, ...
in Main()
sPage = ...
in Main()
sPage = ...
Have you ever noticed that software is never advertised using the adjective "spreadable".
Re: Homework question
Yes, but it also returns "garbage" which I don't use
Re: Homework question
Here's the site! Sorry!
https://www.horoscop.ro/horoscop-berbec/
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.
https://www.horoscop.ro/horoscop-berbec/
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: Select all
<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>
- BruceSteers
- Posts: 1608
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Homework question
Please explain exactly what you need better. and what your experience with gambas is.twbro54l wrote: ↑Thursday 26th January 2023 4:51pm 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
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
Dim aLines As String[]
Dim iCount As Integer
aLines = Split(sPage, "\n") ' split the page into lines.
For iCount = 0 To aLines.Max
' Use the Like keyword to check a string pattern
If aLines[iCount] Like "<meta property=\"og:description\" content=*" Then
Print "Found Line";; aLines[iCount]
Print "The next line is "; aLines[iCount + 1]
Break ' exit the loop as we are finished
Endif
Next
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. ... nt/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)
If at first you don't succeed , try doing something differently.
BruceS
BruceS
- BruceSteers
- Posts: 1608
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Homework question
This will extract the contents of the meta tag you are seeking.
Prints...
Public Sub Form_Open()
Dim hClient As HttpClient 'To create a HTTP Client
Dim sResult As String 'To store the word's meaning
hClient = New HttpClient As "hClient" 'Create a HTTP Client
With hClient 'With the Client..
.URL = "https://www.horoscop.ro/rac/" 'Set up the URL
.Async = False 'No Asynchronous transmission?
.TimeOut = 60 'Don't hang around waiting for more than 60 seconds
.get 'Get the data
End With
While Lof(hClient) ' Lof has a value until all the file has been downloaded
sResult = hClient.ReadLine() ' Read 1 single line
If sResult Like "<meta property=\"og:description\" content=*" Then ' Here the line is found
sResult = Mid(sResult, InStr(sResult, "content=") + 9) ' trim out the left side
sResult = Mid(sResult, 1, RInStr(sResult, "\"") - 1) ' trim out the right side
hClient.Close ' close the stream
Break ' exit the loop
Endif
Wend
Print sResult
End
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
If at first you don't succeed , try doing something differently.
BruceS
BruceS
Re: Homework question
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!
Ill post updates when I'm done. Thank you guys so much!