Link to home
Start Free TrialLog in
Avatar of cameraguy01
cameraguy01

asked on

Reading data from another website (300 pts)

I have a webpage that I am currently developing and I am trying to put the weather on it. I do not want to get the reading from instruments on location but instead get the general weather from a website. I have two different things that I am working on to try and get this to work:

ASP

<%@ Language="VBScript" %>
<%
     Dim whichfile, Content, fs, thisfile
      Content = ""
      Set fs = Server.CreateObject("Scripting.FileSystemObject")
      Set thisfile = fs.OpenTextFile("http://weather.allrefer.com/washington/harrington.html", 1, False)
      Content = thisfile.readall
      thisfile.Close

      set thisfile = Nothing
      set fs = Nothing

      T1.text = content
%>

VBScript

public sub loadweather()
Dim filesys, testfile
dim html as string
Set filesys = CreateObject("Scripting.FileSystemObject")
Set testfile= filesys.OpenTextFile("http://weather.allrefer.com/washington/harrington.html", forreading, TristateUseDefault)
T1.text = testfile.readall
testfile.Close
end sub

Nothing happens with the VBScript and the ASP says:

Microsoft VBScript runtime error '800a0034'
Bad file name or number
/tests/vbs.asp, line 15

I don't care what I end up getting to work. Putting the text into T1 is just a test to see if the text got there. When it does I will use a substring, indexof, or something like that to pick out what I need, I got that part. I would almost prefer it to be in VBScript, just because I understand it better and could probably learn more from it, but it does not matter.

Thank you
Avatar of venkateshwarr
venkateshwarr


I dont think you can give a url for filesystem object
ASKER CERTIFIED SOLUTION
Avatar of venkateshwarr
venkateshwarr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cameraguy01

ASKER

I think that we are close. I first put in the code how you gave it to me and it put the page in before T1 (which is the first and only thing on the test page). Then I deleted the Response.Write xml.responseText and uncommented the T1.text = xml.responsetext and when I tried it, it gave me this:

Microsoft VBScript runtime error '800a01a8'
Object required: 'responseText'
/tests/vbs.asp, line 26

If we can get it into T1 I think I can take it from there, obviously it is reading the page.
the code is working fine for me.

what is T1.text?
Is it a part of asp code or VBscript code...
T1 is a text input on the page just to test to see if the html of the page is there. I think that T1.text is not what I was looking for, I think it was T1.value but I just tried that and it did not work either.
Just to clarify this in case we are unclear, I am looking for the end result of this script to be a string of the HTML of the weather page that it opens. From that I can use the substring and indexof methods to pull out the current weather temperature at the time and display it on the page as I choose.
xml.responseText contains the whole html file as a  string.
if you are getting any errors you might have to install Microsoft.XMLHTTP or MSXML2.ServerXMLHTTP on your server.
Your problem is with an undefined T1.text

I run the script as venkateshwarr has given it OK, and if I add this line
strT1 = xml.responseText
then I can parse strT1, and display the parts of it I want.

Nice one venkateshwarr!
As an aside, I am surprised that it does work for this particular site, as it seems to be a kind of framed site, and I would have thought that the text in the 'frames' would be inaccesible, but I can find any text I want in there so it does all get loaded in the string first. amazing.
If you want weather, why not just subscribe to one of the many weather services? Or use an XML feed directly?
Yes, webwoman, but what if you want to display the weather in your website? That is what cameraguy is trying to achieve, by parsing a wether site, filtering out the information required and presenting that in his own webpage.

So using the xml method, he can now download the weather site as a string, parse it for the temperature say, and only display that piece of information. Pretty neat. i wonder what the implications are though if the weather site is off line or responding slowly. What will the penalty be then?
I think webwoman has made a good suggestion, try contacting them and ask if they can provide something in XML format. The parsing may not work, if they update the website.
Everytime they update you have to update your page...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Alright, some answers for everyone

First, if I had to install Microsoft.XMLHTTP or MSXML2.ServerXMLHTTP then why would the weather page display on my page when I had the Response.Write xml.responseText part in there. Second, I don't see why I would be dealing with an undefined T1.Text. T1 is created before the script to put in the weather is run so it should be there. I might be able to get the text of the page into a variable but how do I display it to test? I get errors when I try using msgbox and I can't use a text input. Third, I realize that there would be problems with the page updating, I have thought about this. When I read through webwoman's comment I looked at the pages that she gave me. When I looked at NWS I saw that on this page (http://weather.noaa.gov/weather/current/KGEG.html) I could probably use a relative reference (substring + indexof) with 'latest' in that list toward the bottom. Webwoman and venkateshwarr are right, an XML feed would be better and I might have to contact them and ask them for that, but for now I think I want to try this. Finally, I do not want to get weather from these websites through their little box, image things. It is not that I have a problem with giving these websites their due, but I strongly feel that it is things like that that make a website look tacky. I try to keep a very consistent feel to pages I design and a big box that advertises for them (right there) with a feel of its own is not something I want. Besides, this might be in a scrolling display of messages along with time and such.

Thank you all for your comments

(I might be increasing the points and splitting them, everybody has brought something to this)
I thought it might help if I included the code of my test page, just to see how things are.

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>VBS</title>
</head>
<body>
<p><input type="text" name="T1" size="20"></p>
<%@ Language="VBScript" %>
<%
  Response.Buffer = True
  Dim objXMLHTTP, xml
  dim strT1

  ' Create an xmlhttp object:
  Set xml = Server.CreateObject("Microsoft.XMLHTTP")
  ' Or, for version 3.0 of XMLHTTP, use:
  ' Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")

  ' Opens the connection to the remote server.
  xml.Open "GET", "http://weather.allrefer.com/washington/harrington.html", False
     
  ' Actually Sends the request and returns the data:
  xml.Send
  strT1 = xml.responseText
  'T1.value = strT1.substring(50,5)    
  Set xml = Nothing
%>
</body>

</html>
Hi CameraGuy01,

Your code should work, but it's a little sloppy ;)
You declare a variable called objXMLHTTP, but you never use it.
Instead you use the variable "xml" for your xml object.
I think that's a dangerous name, because it is also a property of the same (XMLHTTP) object. try this:

<%@ Language="VBScript" %>
<%
  Response.Buffer = True
  Dim objXMLHTTP
  Dim strT1

  ' Create an xmlhttp object:
  Set objXMLHTTP= Server.CreateObject("Microsoft.XMLHTTP")
  ' Or, for version 3.0 of XMLHTTP, use:
  ' Set objXMLHTTP= Server.CreateObject("MSXML2.ServerXMLHTTP")

  ' Opens the connection to the remote server.
  objXMLHTTP.Open "GET", "http://weather.allrefer.com/washington/harrington.html", False
     
  ' Actually Sends the request and returns the data:
  objXMLHTTP.Send
  strT1 = objXMLHTTP.responseText
  'T1.value = strT1.substring(50,5)    
 %>

<% Response.Write(strT1)%>

<%Set objXMLHTTP= Nothing%>

Please note that I left out the HTML tags you use in your version. I did that on purpose because the responseText will give you the -entire- HTML page, including <head> and <body>.  So if you response.write it in the body of an already existing page, you will get double tags.

If you save the code above to an .asp page and run it, you SHOULD see the weatherpage you are trying to reach. When you get to that point you may want to filter the HTML code you get back so you only show the part(s) that you need.

There are some functions for doing exactly that on this page:
http://www.precompiled.com/programming/ 

(scroll to the bottom)