4

How do I get weather results using cURL, assigning a variable to it for later use? Something like:

# This doesn't work and is only a reference for what I want:

  set mytemperature= curl www.yahoo.com/(mycity)/weather.temperature  
  echo %mytemperature%
  • 1
    Do you mean with or without installing additional software? In Windows 10 you could use curl command but Windows 8 doesn't come with curl by default. – Ricardo Bohner Oct 19 at 11:42
  • @RicardoBohner I can definitely do that. You can answer please. – batchcoding____s Oct 19 at 11:48
  • @RicardoBohner I installed cURL and edited the question. Thanks for nudging in the right direction. – batchcoding____s Oct 19 at 12:20
  • More than likely any commercial internet site hosting this info will require you to use some sort of API to access their data/services programmatically. – Hefewe1zen Oct 19 at 12:26
4

enter image description here


To save descriptions and temperature in variable from your bat file, also removing these two characters ┬░ (in hex [0xB0 and 0xC2) from wttr.in output London: +13┬░C and also get back the degree sign º in you var:

@echo off 

cd /d "%~dp0" && setlocal EnableDelayedExpansion

>nul chcp 437 && set /p "_city=Please, enter some location, city, an attraction: " 
for /f "delims= " %%d in ('forFiles /p "." /m "%~nx0" /c "cmd /c echo(0xF8"')do set "_o=%%~d"

for /f tokens^=* %%i in ('^<con: curl https://wttr.in/%_city: =+%^?format^=%%l:+%%t\n -s
')do for /f %%T in ('^<con: cmd /u /c "echo\%%~i"^<nul^|find/v "%_o%"^|findstr /v ^,"
')do set "_dt=!_dt!%%~T"

set "_description_temperature=!_dt::=: !" && call echo\!_description_temperature:+=!!_o!C

timeout -1 & endlocal & goto :eof 
  • Outputs:
London: +13°C

rem :: char      hex code
rem ::  ░    ==   0xB0   // removed in loop
rem ::  ┬    ==   0xC2   // removed in loop
rem ::  °    ==   0xF8   // set _description_temperature=!_dt::=: !!_o!


Old//

  • In PowerShell:

You can use PowerShell solution from @igor_chubin

(curl http://wttr.in/riodejaneiro -UserAgent "curl" ).Content
  • In command line :
powershell -nop -c "(curl http://wttr.in/riodejaneiro -UserAgent "curl" ).Content"
  • In batch file:
@echo off && title <nul && title %~nx0 && mode 128,45

powershell -nop -c "(curl http://wttr.in/riodejaneiro -UserAgent "curl" ).Content"
  • In Windows with curl:
curl http://wttr.in/riodejaneiro 

Obs.: When using whiteout provide some location (curl http://wttr.in), your current location will be assumed to display the data. ...

  • You also can use the command line:
set _temp=cmd /a /v /c "curl wttr.in/RioDeJaneiro?format=^%t --silent"

%_temp%

You can also define outputs:

> curl wttr.in/London?format=%l:+%t\n
London: +13°C
    c    Weather condition,
    C    Weather condition textual name,
    h    Humidity,
    t    Temperature (Actual),
    f    Temperature (Feels Like),
    w    Wind,
    l    Location,
    m    Moonphase 🌑🌒🌓🌔🌕🌖🌗🌘,
    M    Moonday,
    p    precipitation (mm),
    P    pressure (hPa),

    D    Dawn*,
    S    Sunrise*,
    z    Zenith*,
    s    Sunset*,
    d    Dusk*.

        (*times are shown in the local timezone)

    $ curl wttr.in/London?format=3
    London: ⛅️ +7⁰C
    $ curl wttr.in/London?format="%l:+%c+%t\n"
    London: ⛅️ +7⁰C

If you want to know the name of one of the coldest permanently inhabited locales on the planet and get the weather in there:

curl wttr.in/*
| improve this answer | |
  • But how do I set the value of Temp./Weather to a Variable? – batchcoding____s Oct 19 at 14:15
  • @batchcoding____s see edit... – It Wasn't Me Oct 19 at 14:35
4

Get the temperature into a variable:

@echo off
chcp 1252 > nul
::Put your city here:
set City=Rio de Janeiro

set City_=%City: =-%
for /f "Delims=" %%a in ('curl --silent wttr.in/%City_%?format^=%%t') do set "CTemperature=%%a"
set CTemperature=%CTemperature:+=%
set CTemperature=%CTemperature:~0,-3%
echo The current temperature in %City% is %CTemperature% º Celcios
echo.
pause

Get temperature and weather description into variables:

@echo off
chcp 1252 > nul
::Put your city here:
set City=Rio de Janeiro

set City_=%City: =-%
for /f "Delims=" %%a in ('curl --silent wttr.in/%City_%?format^=%%t') do set "CTemperature=%%a"
set CTemperature=%CTemperature:+=%
set CTemperature=%CTemperature:~0,-3%
for /f "skip=1 tokens=4*" %%a in ('curl --silent wttr.in/%City_%?0') do set "Description=%%a %%b"& goto :Next
:Next
echo.
echo The current temperature in %City% is %CTemperature% º Celcios "%Description%"
echo.
pause
| improve this answer | |
  • Works like a Charm! Just can we also get the weather in a variable - like Cloudy or Raining? – batchcoding____s Oct 19 at 14:23
  • I edited the answer – Ricardo Bohner Oct 19 at 14:50
  • To get a list of the options you can use in this website you can go to wttr.in/:help – Ricardo Bohner Oct 19 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.