[RESOLVED] Reading from website through VB
Hi,
I am developing a program that needs to read certain values from a specific website for further calculations. For example, suppose I need to read todays maximum and minimum temperatures from certain weather sites and want to report the arithmatic mean of them in a msgbox. Is it possile? If not directly, then as a workaround, if I can save the website as text file, I can read that file and find the value I want into a variable. But how to save it in the background so that the user does not see it.
Please help.
Regards.
Re: Reading from website through VB
Hai arnabbando,
did not you try inet control (internet transfer control) ?
that would do the work. also webbrowser control.
tons of example can be found in the vbf code bank.
Re: Reading from website through VB
I have tried it with INet. Though I am programming in VB6 for quite some times now, but never needed to interact with internet before. I got the idea of using INet by seraching the vbforums only. Now when I am trying the following:
Code:
Dim strText As String
strText = Inet1.OpenURL("http://www.wunderground.com/global/stations/42060.html")
Open App.Path & "\test.txt" For Output As #1
Print #1, strText
Close #1
What I am getting in the text file is this:
Quote:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Refresh" content="1800;URL=/global/stations/42060.html?MR=1" />
<meta name="ICBM" content="32.33000183, 75.50000000" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="pics-label" content='(pics-1.1 "http://www.icra.org/ratingsv02.html" comment "ICRAonline v2.0" l gen true for "http://www.wunderground.com" r (nz 0 vz 0 lz 0 oz 0 cz 0) "http://www.rsac.org/ratingsv01.html" l gen true for "http://www.wunderground.com" r (n 0 s 0 v 0 l 0))' />
<title>Kathua, India Forecast : Weather Underground</title>
<link rel="alternate" type="application/rss+xml" title="Kathua, India RSS" href="http://rss.wunderground.com/auto/rss_full/global/stations/42060.xml" />
<link rel="stylesheet" type="text/css" href="http://icons-pe.wxug.com/css/wu2_base.css?v=2008060201" />
<link rel="stylesheet" type="text/css" href="http://ic
Offcourse this is not what I want. If you open the website http://www.wunderground.com/global/stations/42060.html in a browser, it is the five days forecast maximum and minimum temperatures that I want to read.
Please help me.
Re: Reading from website through VB
If you have some knowledge on HTML basics, then you can also use MS Html object library to get the text in that webpage you shown. The values are kept inside <span> . so you can just retrive the inner text part of it.
since the <span> element in that webpage does not contain an ID atribute, i am not sure how to identify each span element uniquely.
so what i mean is, you can use the MS HTML Object library to retrieve that values.
Re: Reading from website through VB
I will try that. Thanks.
However, meanwhile I tried to save the website from the browser as HTML and opened it through Microsoft FrontPage to view the HTML code behind. There the first few lines are matching with the text file I have posted earlier. But the whole code is much more than that. In the code, later it contains the temperature values which I think I can identify and read out.
So what I want to ask is if it has something to do with maximum length of a string variable or something like that.
Give me a clue.
1 Attachment(s)
Re: Reading from website through VB
Hope this example might give you some idea.
Also this link. http://www.w3schools.com/HTMLDOM/default.asp
Re: Reading from website through VB
Quote:
Originally Posted by arnabbandyo
Would something like this help get you started?
http://img371.imageshack.us/img371/7293/5dayqi2.png
Re: Reading from website through VB
Hi Edgmeal,
Thanks for the post. i have one question also.
Quote:
If you have some knowledge on HTML basics, then you can also use MS Html object library to get the text in that webpage you shown. The values are kept inside <span> . so you can just retrive the inner text part of it.
since the <span> element in that webpage does not contain an ID atribute, i am not sure how to identify each span element uniquely.
When an Element has no ID specified, is there a way to uniquely identify it?
Re: Reading from website through VB
Quote:
Originally Posted by Fazi
Hi Edgmeal,
Thanks for the post. i have one question also.
When an Element has no ID specified, is there a way to uniquely identify it?
I really don't know much at all about that stuff. If you look at the code I posted above it just uses the webbrower control to return the text of the webpage (html tags are removed), trims off some junk and the parses that text to get the result I was trying to archive.
Edit The code can easily break if the web page changes,
I forgot to finish the code so Celsius works....
Code:
Counter = 0
For i = LINES To UBound(sArray)
If InStr(1, sArray(i), " C") > 0 Then ' Changed " F" to " C" for Celsius
Counter = Counter + 1
If Counter = 1 Then
Text1 = Text1 & sArray(i) & "/"
TEMPS = TEMPS + 1
ElseIf Counter = 2 Then
Text1 = Text1 & sArray(i) & vbTab
Counter = 0
TEMPS = TEMPS + 1
End If
If TEMPS = 10 Then Exit For
End If
Next i
Re: Reading from website through VB
OK Edgemeal,
Actually if we can do what i was telling, that trimming also not requir. just we can directly get the text from the <span> eliment.
Any way, what you have given is owsome.
Re: Reading from website through VB
What you have done will not only get me started, I think it is enough for me to finish the job.
I don't have language to thank you.
However, I knew that vbforums will be able to solve my problem since whatever I have learnt in VB is from this site only.
Re: Reading from website through VB
Quote:
Originally Posted by Fazi
Hope this example might give you some idea.
thank u so much Fazi. i was looking for an example like u gave. it solved one of my projects too but i have a question. i need to fill two textfields in a website but their name's are same in code. when i run ur code in mine, only first of them is filled. do u have a solution for that?
and this is its code:
Account Name: <input type='text' class="textfield" name='account'><br />
<input type='image' target="_blank" src="images/top100.jpg" border="0" value='Vote' alt="Submit button" />
</form>
<br />
<form action='vote/vote.php' target="_blank" method='post'>
Account Name: <input type='text' class="textfield" name='account'><br />
<input type='image' target="_blank" src="/images/xtremetop100.jpg" border="0" value='Vote' ALT="Submit button" /></form>
</form>
Re: [RESOLVED] Reading from website through VB
iRon,
Hm, If i find a solution, i ll come back ok :)