How to get weather and weather information?

Asked

Viewed 20,421 times

8

I came across a problem I’d like to see if it can be solved similar to the post office. When you have a zip code you can rescue from a web service other data, such as the address and city of that zip code.

Regarding the weather, there is some way with information from the place to pick up the temperature of the weather forecast, the time and the current temperature of that place (the intention later was to style the css of that part of the page to leave a nice look)?

  • There are several widgets on the site of Climatempo and Tempo Agora. Gives a search.

  • 1

    but I didn’t want something ready Diego, as I made clear in the question, I wanted to take the information via json for example, something similar as it is done in the post office, for later I manipulate and create my own widget, but I will take a look at these sites also to see how it is.

  • 1

    "see if it can be solved in a similar way to that of the post office" - precisely, making a proper Rawler can be the way. You need to see if the site license allows it. " I can neither deny nor confirm" that I do this currently with a quality source, but somehow I quote the source in the final result, I imagine that I have no problems with the "license" in this way kkk.

6 answers

8


You get this information through meteorological services Apis, such as Yahoo!

I created and tested an example of usage as shown below:

<script>
    var callbackFunction = function(data) {
        console.log(data);
    };
</script>

<script src="https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='são paulo, sp')&format=json&callback=callbackFunction"></script>

Where:

  • format: is the data type that will be returned, can be JSON or XML;
  • callback: is the function that will handle the response to your request.

You can also add

and u = 'c'

in the WHERE clause to change to the metric system and scale to Celsius.

If it is only a city, you can search WOEID before to improve response time.

For example, for São Paulo capital,

http://woeid.rosselliot.co.nz/lookup/sao%20paulo

Would return 455827, where the final query would be:

select * from weather.forecast where woeid = 455827&format=json&callback=callbackFunction

There is this website, Yahoo! itself, where you can test your YQL (Yahoo Query Language).

It is worth saying that according to the Yahoo API, the daily query limit is 2,000 requests, and be aware of the terms of use, where it can only be used by individuals, non-profit companies and non-commercial use.


Remember that the Yahoo! API is just one of the solutions, if it doesn’t meet your needs, you can search for 'Weather api' or 'weather forecast api'.

  • I’ll test this silvalexandre solution and then I’ll tell you if I can use it

  • It took me a while to see rs, but it’s working fine!

5

I found this 'API' which returns the following data:

{
 "cidade":"Sao Paulo - SP",
 "agora":{
  "data_hora":"08\/11\/2013 - 19:59",
  "descricao":"Muito Nublado (noite)",
  "temperatura":"19",
  "humidade":"73%",
  "visibilidade":"9,99 km",
  "vento_velocidade":"20,92 km\/h",
  "vento_direcao":"SE",
  "pressao":"1.015,92 mBar",
  "pressao_status":"subindo",
  "nascer_do_sol":"6:16 am",
  "por_do_sol":"7:25 pm",
  "imagem":"http:\/\/developers.agenciaideias.com.br\/images\/tempo\/27.png"
 },
 "previsoes":[
  {
   "data":"Sexta - 08\/11\/2013",
   "descricao":"Parcialmente Nublado (noite)",
   "temperatura_max":"23",
   "temperatura_min":"16",
   "imagem":"http:\/\/developers.agenciaideias.com.br\/images\/tempo\/29.png"
  },
  {
   "data":"S\u00e1bado - 09\/11\/2013",
   "descricao":"Tempo Bom (dia)",
   "temperatura_max":"27",
   "temperatura_min":"18",
   "imagem":"http:\/\/developers.agenciaideias.com.br\/images\/tempo\/34.png"
  },
  {
   "data":"Domingo - 10\/11\/2013",
   "descricao":"Ensolarado",
   "temperatura_max":"29",
   "temperatura_min":"21",
   "imagem":"http:\/\/developers.agenciaideias.com.br\/images\/tempo\/32.png"
  },
  {
   "data":"Segunda - 11\/11\/2013",
   "descricao":"Parcialmente Nublado (dia)",
   "temperatura_max":"32",
   "temperatura_min":"23",
   "imagem":"http:\/\/developers.agenciaideias.com.br\/images\/tempo\/30.png"
  },
  {
   "data":"Ter\u00e7a - 12\/11\/2013",
   "descricao":"Parcialmente Nublado (dia)",
   "temperatura_max":"30",
   "temperatura_min":"19",
   "imagem":"http:\/\/developers.agenciaideias.com.br\/images\/tempo\/30.png"
  }
 ]
}

Follow the url: http://developers.agenciaideias.com.br/tempo

I tested the request described in the documentation, with Angularjs, and returned everything straight. =)

  • 3

    This one agency has "api" for a lot of stuff and doesn’t say the data sources. I cannot say this, but it gives me the impression of being a mere collection of third-party data obtained by crawlers. And crawlers usually break when the original source changes structure, in addition to not necessarily respecting the rules of origin use. Probably I would never use such a solution (now, if I’m wrong, just point me where this information is, I change my mind, and still give a compliment for the diversity of information).

  • Not to mention it’s no longer working rs... at least not today

  • @Bacco what it would be crawlers?

  • 1

    @Jorgematheus also called scrapers, depends on the context. They are applications that access the data as if they were normal users browsing the site or application, and remove the data from the pages via string operations. As it is not an "official" process, and the pages always change, they depend on constant updating of who made Crawler, and often violate the permission to use the original website.

5

Climatempo (Brazilian meteorological company) launched an API in 2017, you can preach the forecast for free..

The site is https://advisor.climatempo.com.br

There is all the documentation. Just enter your "Facebook", "Google" or "Github", generate a token and request via GET.

  • Excellent collaboration Vitor! Very good indeed :)

2

If it is via programming with c# recommend using the Html Agility Pack, with it Voce can tell the origin of the information (Site) and inform the code in which divide this information you want being able to use for various purposes, I used to get the results of the mega sena.

http://html-agility-pack.net/? z=codeplex

1

Based on Silvalexandre’s response
Use the link before to discover the woeid of your location

    select woeid from geo.places(1) where text='são paulo, sp'

Javascript Calls the lib jquery and

    <script>
    $.get('https://query.yahooapis.com/v1/public/yql',{
        q:'select * from weather.forecast where woeid in (455822)',
        format:'json'
    },function (res) {
        if(res){
            umi = res.query.results.channel.atmosphere.humidity;
            temp = res.query.results.channel.item.condition.temp;

            temp = Math.round((temp-32)/1.8); //F to C

            console.log(temp,umi);
        }
    },'json');
    </script>

PHP

$cont = file_get_contents("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(455822)&format=json");

$cont = json_decode($cont,1);

$umi = $cont['query']['results']['channel']['atmosphere']['humidity'];
$temp = $cont['query']['results']['channel']['item']['condition']['temp'];

$temp = intval( ($temp - 32) /1.8 ); //F to C

echo $umi." ".$temp;

1

  • I cannot consume data from that url

Browser other questions tagged

You are not signed in. Login or sign up in order to post.