Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I recently got this script to get weather forecast,

Status
Not open for further replies.

Jerkycold

IS-IT--Management
Jun 17, 2003
26
0
0
US
How can I get this to work? I am new to cold fusion and I cannot seem to get this to work. What am I doing wrong?

<cfhttp url=&quot; method=&quot;GET&quot;>

</cfhttp>
<!--- Extract preformatted results --->
<cfset text = #CFHTTP.FileContent#>
<cfset begin = Find(&quot;Current Conditions</font></a></font><br>&quot;,&quot;#text#&quot;)>
<!--- if incorrect results are returned, print error --->
<cfif #begin# lt 1>
<cfoutput>
<b>An Error occured, please try again. (Make sure to specify a proxy server if you are behind a firewall.)</b>
</cfoutput>
<cfabort>
</cfif>
<cfset begin = begin + 51>
<cfset output = RemoveChars('#CFHTTP.FileContent#',1,begin)>
<cfset nofooter = Find(&quot;<!-- 5 DAY HTML END -->&quot;,&quot;#output#&quot;)>
<cfset output = RemoveChars('#output#',#nofooter#,20000)>
<!--- Output preformatted results --->
<table width=450>
<tr>
<td>
<cfoutput>#output#</cfoutput>
</td>
</tr>
</table>
 
What doesn't work? Are you populating the zipcode in the query string?

Did you check the documentation where you got the script?

DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
How do I do that? here is the documentation:

In order to use this code, simply place it
in your coldfusion tags dir. or withing the dir. of the site, and
use <CF_FXWeather
zipcode=#form.zipcode#> - assuming it was passed from a form

How do I do this? Can you send me a example?
 
Create a page such as:

weather.cfm-
Code:
<CFPARAM name=&quot;FORM.zipcode&quot; default=&quot;01776&quot;>
<html>
<head><title>The Weather for zipcode <CFOUTPUT>#FORM.zipcode#</CFOUTPUT></title>
</head>

<body>

<h2>The Weather for zipcode <CFOUTPUT>#FORM.zipcode#</CFOUTPUT></h2>

<CF_FXWeather zipcode=&quot;#form.zipcode#&quot;>

<CFOUTPUT>
<form action=&quot;#GetFileFromPath(GetBaseTemplatePath())#&quot; method=&quot;POST&quot;>
  Enter your zipcode:
  <input type=&quot;text&quot; name=&quot;zipcode&quot; value=&quot;#FORM.zipcode#&quot;>
  <input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Go&quot;>
</form>
</CFOUTPUT>

</body>

put the Custom Tag you downloaded (FXWeather.cfm) into the Custom Tags directory of your ColdFusion server, or, alternately, put it in the same directory as weather.cfm.

Then simply bring up weather.cfm in your browser. Whatever value you have it the CFPARAM tag will be the default zipcode... alter it as you see fit.



-Carl
 
Thanks for your help, it actually does not give me errors now except it outputs the default error in the script

an error occured, please try again. (make sure to specify a proxy server if you are behind a firewall.)


Did you get this to work correctly? Did you get a weather forecast?
 
Yes, it looks like Accuweather changed the format of their page. This happens a lot, unfortunately... that's why grabbing content like this isn't terribly reliable.

Take a look at
The string that the original code was looking for - &quot;Current Conditions&quot; - appears nowhere on the page... so the whole script fails.

What you need to do is look at the source for the page (in it's current format), and figure out what you can parse as boundaries for the content you actually want... and then rewrite the code to look for those boundaries.

A quick example would be:
Code:
<CFPARAM name=&quot;URL.zipcode&quot; default=&quot;01776&quot;>

<CFHTTP url=&quot;[URL unfurl="true"]http://www.accuweather.com/adcbin/local_index?thisZip=#URL.zipcode#&quot;[/URL] method=&quot;GET&quot;>
</CFHTTP>

<!--- Extract preformatted results  --->
<CFSET sStartText = &quot;<!-- content table 3 --->&quot;>
<CFSET sEndText = &quot;<!--   End - Forecast Cell -->&quot;>

<CFSET forecast = CFHTTP.FileContent>
<CFSET nBegin = FindNoCase(sStartText,forecast)>

<!--- if incorrect results are returned, print error --->
<CFIF nBegin LTE 0>
    <CFSAVECONTENT variable=&quot;nOutput&quot;>
	<h2>Error: 1 - No beginning boundary found</h2>
    <b>An Error occured, please try again. (Make sure to specify a proxy server if you are behind a firewall.)</b>
    </CFSAVECONTENT>
<CFELSE>

	<CFSET nBegin = nBegin + Len(sStartText)>
	<CFSET nOutput = RemoveChars(CFHTTP.FileContent,1,nBegin)>
	<CFSET nEnd = FindNoCase(sEndText,nOutput)>
	<CFIF nEnd LTE 0>
    	<CFSAVECONTENT variable=&quot;nOutput&quot;>
		<h2>Error: 2 - No ending boundary found</h2>
	    <b>An Error occured, please try again. (Make sure to specify a proxy server if you are behind a firewall.)</b>
	    </CFSAVECONTENT>
		
	<CFELSE>

		<cfset nOutput = RemoveChars(nOutput,nEnd,Len(nOutput))>
		
		<!--- get rid of the pesky javascript --->
		<CFSET nOutput = ReplaceNoCase(ReplaceNoCase(nOutput,&quot;</SCRIPT>&quot;,&quot;</SCRIPT --->&quot;,&quot;ALL&quot;),&quot;<SCRIPT&quot;,&quot;<!--- SCRIPT&quot;,&quot;ALL&quot;)>
		
		<!--- repair the mismatched table tags --->
		<CFSET nOutput = nOutput & &quot;</td></tr></table>&quot;>
	</CFIF>
	
</CFIF>

<!--- Output preformatted results  --->
<table width=450>
<tr>
    <td>
    <cfoutput>#nOutput#</cfoutput>
    </td>
</tr>
</table>

I scanned through very quickly and found two strings that I thought were fairly decent boundaries...
Code:
&quot;<!-- content table 3 --->&quot;
and
Code:
&quot;<!--   End - Forecast Cell -->&quot;
. Then the above code begins to construct nOutput which contains just the content I want. I ran nOutput through Tidy to make sure it had matching tags everywhere (it didn't, so I added what I needed to at the end)... and there you have it.

But you really need to understand the above code, and get really good at editing it. Because you'll probably have to revisit it often, as Accuweather is constantly changing their look-and-feel, and the strings you searched for last week for your boundaries might not exist next week.


The other, much easier, and much more reliable way to do it is to simply grab the whole page and put it into a frame within your site's UI. This way you also don't step on any copyright toes... so it's also the better &quot;web community&quot; way to do it.




-Carl
 
... and, actually, if I were going to do this for real... I'd probably use the &quot;Printer Friendly&quot; version of the page instead. Seems much easier to parse... and no javascript.

Code:
<CFPARAM name=&quot;URL.zipcode&quot; default=&quot;01776&quot;>

<CFHTTP url=&quot;[URL unfurl="true"]http://wwwa.accuweather.com/adcbin/public/local_index_print.asp?partner=accuweather&zipcode=#URL.zipcode#&metric=0&quot;[/URL] method=&quot;GET&quot;>
</CFHTTP>

<!--- Extract preformatted results  --->
<CFSET sStartText = &quot;<!--   Start - Forecast Cell -->&quot;>
<CFSET sEndText = &quot;<!--   End - Forecast Cell -->&quot;>

<CFSET forecast = CFHTTP.FileContent>
<CFSET nBegin = FindNoCase(sStartText,forecast)>

<!--- if incorrect results are returned, print error --->
<CFIF nBegin LTE 0>
    <CFSAVECONTENT variable=&quot;nOutput&quot;>
	<h2>Error: 1 - No beginning boundary found</h2>
    <b>An Error occured, please try again. (Make sure to specify a proxy server if you are behind a firewall.)</b>
    </CFSAVECONTENT>
<CFELSE>

	<CFSET nBegin = nBegin + Len(sStartText)>
	<CFSET nOutput = RemoveChars(CFHTTP.FileContent,1,nBegin)>
	<CFSET nEnd = FindNoCase(sEndText,nOutput)>
	<CFIF nEnd LTE 0>
    	<CFSAVECONTENT variable=&quot;nOutput&quot;>
		<h2>Error: 2 - No ending boundary found</h2>
	    <b>An Error occured, please try again. (Make sure to specify a proxy server if you are behind a firewall.)</b>
	    </CFSAVECONTENT>
		
	<CFELSE>

		<cfset nOutput = RemoveChars(nOutput,nEnd,Len(nOutput))>
		
	</CFIF>
	
</CFIF>

<!--- Output preformatted results  --->
<table width=450>
<tr>
    <td>
    <cfoutput>#nOutput#</cfoutput>
    </td>
</tr>
</table>


-Carl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top