如何使用 获取天气结果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.in
London: +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