The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Weather icon extraction
Discuss Weather icon extraction in the PHP Development forum on Dev Shed. Weather icon extraction PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 23rd, 2012, 12:54 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 12 m 33 sec
Reputation Power: 0
|
|
Weather icon extraction
I have on my website weather data from my personal weather station. Only thing it's missing its weather conditions. I would like to extract the weather condition icon from here
Code:
http://www.weather.com/weather/extended/PLXX0040?par=yahoo&site=www.yahoo.com&promo=extendedforecast&cm_ven=Yahoo&cm_cat=www.yahoo.com&cm_pla=forecastpage&cm_ite=CityPage
(Right Now window). Is there any way to do this?
|

December 23rd, 2012, 12:57 PM
|
|
|
|
Yes.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

December 23rd, 2012, 01:49 PM
|
 |
Contributing User
|
|
|
|
You can do this with PHP Simple HTML DOM Parser
For example:
PHP Code:
include("simple_html_dom.php");
$html = file_get_html('http://www.weather.com/weather/extended/PLXX0040?par=yahoo&site=www.yahoo.com&promo=extendedforecast&cm_ven=Yahoo&cm_cat=www.yahoo.com&cm_pla=forecastpage&cm_ite=CityPage');
echo $html->find('img[class=wx-weather-icon]')->src;
Or with DOMXPath
|

December 23rd, 2012, 02:38 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 12 m 33 sec
Reputation Power: 0
|
|
Thank you for answer. I tried PHP Simple HTML DOM Parser - downloaded that and uploaded on the server. Then I created new file and put into:
Code:
<?php include("/includes/simple_html_dom.php"); $html = file_get_html('http://www.weather.com/weather/extended/PLXX0040?par=yahoo&site=www.yahoo.com&promo=extendedforecast&cm_ven=Yahoo&cm_cat=www.yahoo.com&cm_pla=forecastpage&cm_ite=CityPage'); echo $html->find('img[class=wx-weather-icon]')->src; ?>
I uploaded that file and insert the path to it in my browser and I have nothing - I mean blank website. What did I wrong?
|

December 23rd, 2012, 03:03 PM
|
 |
Contributing User
|
|
|
|
I forgot to add a 0 in the $html->find.
Try it like this:
PHP Code:
$weatherimage = $html->find('img[class=wx-weather-icon]', 0)->src;
echo "<img src='".$weatherimage."' alt='weather image' />";
|

December 23rd, 2012, 03:39 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 12 m 33 sec
Reputation Power: 0
|
|
It's working. Thanks
By the way, I noticed that under the icon it's also a short text, eg. "Fog" (
Code:
http://www.weather.com/weather/extended/PLXX0040?par=yahoo&site=www.yahoo.com&promo=extendedforecast&cm_ven=Yahoo&cm_cat=www.yahoo.com&cm_pla=forecastpage&cm_ite=CityPage
).
It would be difficult to print it on my website but first translate it into my language? For instance, I would like to have not 'fog' but its meaning in my country 'mgla'. Thus it should be translated first...
|

December 23rd, 2012, 04:13 PM
|
 |
Contributing User
|
|
|
|
Try this
PHP Code:
$condition = trim($html->find('li[class=wx-phrase]', 0)->plaintext); //Extract the inner text from the <span class='wx-value' and trim off whitespace
switch ($condition) { //Switch between the different weather conditions
case "Fog":
echo "mgla";
break;
case "Light Freezing Rain":
echo "Your translation";
break;
case "Mostly Sunny":
echo "Translation";
break;
default:
echo $condition; //You have not yet translated it so just show the English one
}
It should be noted however that if weather.com changes their site, this script might not work anymore.
|

December 24th, 2012, 04:53 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 12 m 33 sec
Reputation Power: 0
|
|
|
Thank you for your help one more time. Now I have all weather elements as I wanted. Unfortunately I wasn't able to do it alone. I hope weather.com won't change the script soon.
|

December 24th, 2012, 05:29 AM
|
 |
Contributing User
|
|
|
|
Well, if they change it, it won't be so hard to change the script. I bet you can figure it out by the examples I just gave you 
|

January 5th, 2013, 10:52 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 12 m 33 sec
Reputation Power: 0
|
|
|
icon's script generates error on my page?
The script worked fine for 2 weeks. Today something started to happen wrong. In the morning I noticed that my page is almost blank. Only weather banner and the text above the icon was visible. There wan't weather icon ( snapshot ). When I deleted file with an icon the webpage opened correctly.
Thus, I know that the reason of an error is the icon's script. However I didn't change nothing recently. So I tried to do something with this and I changed code for the meteo station from weather.com for another station eg. PLXX1335 into PLXX0040. It helped and the icon was visible soon and the whole page too. Unfortunately, after a few hours the error occured again. I changed the code for station and it was the same - icon was visible as well as the whole page only for about 10 minutes. Now I have the icon's file deleted in order to have my site visible.
I don't know what could be the reason?
I attach the code for the script:
PHP Code:
<?php // pokaz pogodowe warunki //pobierz informacje include("simple_html_dom.php"); $html = file_get_html('http://www.weather.com/weather/extended/PLXX1335?par=yahoo&site=www.yahoo.com&promo=extendedforecast&cm_ven=Yahoo&cm_cat=www.yahoo.com&cm_pla=forecastpage&cm_ite=CityPage'); $weatherimage = $html->find('img[class=wx-weather-icon]', 0)->src; echo "<img src='".$weatherimage."' alt='weather image' />"; $condition = trim($html->find('li[class=wx-phrase]', 0)->plaintext); //Extract the inner text from the <span class='wx-value' and trim off whitespace echo"<br>"; switch ($condition) { //Switch between the different weather conditions case "Fog": echo "mgła"; break; case "Light Freezing Rain": echo "Słaby marznący deszcz"; break; case "Light Freezing Rain / Fog": echo "Słaby marznący deszcz/Mgła"; break; case "Clear": echo "bezchmurnie"; break; case "Cloudy": echo "pochmurno"; break; case "Haze": echo "zamglenia"; break; case "Light Rain": echo "słaby deszcz"; break; case "Mostly Cloudy": echo "<center>zachmurzenie duże</center>"; break; case "Overcast": echo "całkowite zachmurzenie"; break; case "Partly Cloudy": echo "częściowe zachmurzenie"; break; case "Rain": echo "deszcz"; break; case "Rain Showers": echo "opady deszczu"; break; case "Showers": echo "deszczyk"; break; case "Light Rain Shower": echo "lekki przelotny deszcz"; break; case "Thunderstorm": echo "burza"; break; case "Mostly Sunny": echo "przeważnie słonecznie"; break; case "Partly Sunny": echo "częściowo słonecznie"; break; case "Mostly Sunny": echo "słonecznie"; break; case "Sunny": echo "słonecznie"; break; case "Fair": echo "pogodnie"; break; case "Scattered Showers": echo "przelotny deszcz"; break; case "Snow": echo "śnieg"; break; case "Light Snow": echo "słabe opady śniegu"; break; case "Light Rain / Windy": echo "słaby deszcz / wietrznie"; break; default: echo $condition; //You have not yet translated it so just show the English one } ?>
The script responsible for printing the icon on my page (in PHP - here ):
Code:
<div class='ikona'>"; include('includes/icon.php'); echo"</div>
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|