Using the Linux shell to get the weather, it would be difficult, originally, really simple, single-digit code is done.
Get weather for the corresponding city
All weather information is obtained from the China Weather Network. Each city will correspond to an ID (for example, Beijing is 101010100, because I in Yinchuan, so the example of Yinchuan id:101170101), through the ID can obtain the corresponding city real-time weather or all-day weather, but also can get seven days of weather.
Shell Script
The shell script code is as follows:
#!/bin/sh weatherdateroot=http://www.weather.com.cn/data/sk/101170101.html weatherdatafile=wget $weatherDateRoot-o $weatherDataFile >/dev/null2> &1sed's/.*temp ': "\ ([0-9]\{1,2\}\). */\1/g' $ Weatherdatafile
This script obtains the weather information and then matches it to the current temperature by the regular match.
If you only use this script and no more two processing, that's too much trouble. I get the weather information after it is displayed to the terminal command prompt, so I need to do the processing.
The terminal command prompt displays the weather
First get the corresponding city weather, such as Yinchuan corresponding real-time weather information in:
Http://www.weather.com.cn/data/sk/101170101.html
You first search the weather in China, you want to the city's weather, the URL will contain the City weather ID, the above URL to replace the ID of your city to get the ID.
There are also all day weather information:
Http://www.weather.com.cn/data/cityinfo/101170101.html
Do not know China Weather Network provides seven days weather information no? If so, we can also get weather information for seven days by this method.
Get Weather information
corresponding shell script:
#!/bin/shalldataurl=http://www.weather.com.cn/data/cityinfo/101170101.html alldatafile=/home/snowsolf/shell/weather/allday.htmldataurl=http://www.weather.com.cn/ data/sk/101170101.htmldatafile=/home/snowsolf/shell/weather/weather.htmlwget $dataUrl-o $ datafile >/dev/null2>&1wget $allDataUrl-O $allDataFile >/ dev/null2>&1
Timed access
Set the timer task with the crontab command, execute the CRONTAB-E command (if you need to set the default editor for the first time), and then add it at the end of the file:
*/30 * * * */home/snowsolf/shell/weather/weather.sh >>/dev/null
This line of code is set to execute every 30 minutes to get the weather script, specific crontab command Other syntax can Google or Baidu.
Extracting weather
sed ' s/.*temp ":" \ ([0-9]\{1,2\}\). */\1/g '
This command gets real-time weather from the real-time weather file you get.
The terminal command prompt displays
You can refer to http://www.cnblogs.com/snowsolf/p/3192224.html. Here, you can make your command prompt more beautiful.
Last one of my command-prompt graphs:
It's so cool!!! It's good to be a love-toss programmer!
Linux-shell getting weather