如何在 Windows 8.1 中使用 cURL 通过 cmd 获取天气?

如何在 Windows 8.1 中使用 cURL 通过 cmd 获取天气?

如何使用 获取天气结果cURL,并为其分配变量以供日后使用?例如:

# 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

在此处输入图片描述


要将描述和温度保存在文件中的变量中,还要从输出中.bat删除字符┬░(十六进制[0xB0 and 0xC2:)并恢复度数符号 [ ]:wttr.inLondon: +13┬░Cº

  • @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
    
    输出:London: +13°C
    rem :: char       hex code
    rem ::  ░    ==   0xB0   // removed in loop
    rem ::  ┬    ==   0xC2   // removed in loop
    rem ::  °    ==   0xF8   // set _description_temperature=!_dt::=: !!_o!
    


老的:

  • PowerShell:您可以使用以下解决方案@igor_chubin
    (curl http://wttr.in/riodejaneiro -UserAgent "curl" ).Content
    或者
    powershell -nop -c "(curl http://wttr.in/riodejaneiro -UserAgent "curl" ).Content"

  • 批处理文件:使用 whiteout 时,提供一些位置 [curl http://wttr.in],否则您的当前位置将被假定显示数据:
    @echo off && title <nul && title %~nx0 && mode 128,45
    
    powershell -nop -c "(curl http://wttr.in/riodejaneiro -UserAgent "curl" ).Content"
    

  • 命令:
    set _temp=cmd /a /v /c "curl wttr.in/RioDeJaneiro?format=^%t --silent"
    
    %_temp%
    

定义输出:

  • $ 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 

答案2

将温度存入变量:

@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

将温度和天气描述放入变量中:

@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

答案3

curl wttr.in/Mumbai

了解几天的温度

只需使用 curl 命令和您的城市名称即可获取天气报告

这是孟买市的天气预报:
孟买天气 cmd 的图片

相关内容