#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Question 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?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,378
    Rep Power
    594
    Yes.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    105
    Rep Power
    51
    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
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Arrow


    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?
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    105
    Rep Power
    51
    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' />"
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Question


    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...
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    105
    Rep Power
    51
    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.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep 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.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    105
    Rep Power
    51
    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
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep 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>

IMN logo majestic logo threadwatch logo seochat tools logo