Solved

Reading data from another website (300 pts)

Posted on 2004-04-21
16
670 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:cameraguy01
  • 5
  • 5
  • 3
  • +2
16 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
Comment Utility

I dont think you can give a url for filesystem object
0
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 300 total points
Comment Utility
try this...

<%
  Response.Buffer = True
  Dim objXMLHTTP, xml

  ' 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

  Response.Write xml.responseText
 
 '''''T1.text = xml.responseText
 
  Set xml = Nothing
%>

0
 

Author Comment

by:cameraguy01
Comment Utility
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.
0
 
LVL 12

Expert Comment

by:venkateshwarr
Comment Utility
the code is working fine for me.

what is T1.text?
Is it a part of asp code or VBscript code...
0
 

Author Comment

by:cameraguy01
Comment Utility
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.
0
 

Author Comment

by:cameraguy01
Comment Utility
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.
0
 
LVL 12

Expert Comment

by:venkateshwarr
Comment Utility
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.
0
 
LVL 6

Expert Comment

by:sijpie
Comment Utility
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!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:sijpie
Comment Utility
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.
0
 
LVL 19

Expert Comment

by:webwoman
Comment Utility
If you want weather, why not just subscribe to one of the many weather services? Or use an XML feed directly?
0
 
LVL 6

Expert Comment

by:sijpie
Comment Utility
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?
0
 
LVL 12

Expert Comment

by:venkateshwarr
Comment Utility
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...
0
 
LVL 19

Assisted Solution

by:webwoman
webwoman earned 35 total points
Comment Utility
If you want to display the weather, go to www.weatherunderground.com and put in your city. You can have the weather on your site, fed from them.

Or go to www.weather.com, click on weather tools in the top menu, and scroll down and look for 'weather on your web site'

I think the NWS has a text feed, or an XML one.

Why go through all that parsing when weather is available and easy to do?
0
 

Author Comment

by:cameraguy01
Comment Utility
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)
0
 

Author Comment

by:cameraguy01
Comment Utility
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>
0
 
LVL 2

Expert Comment

by:ricosuave
Comment Utility
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)



0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now