自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux-shell獲取天氣

系統(tǒng) Linux
用Linux中的shell獲取天氣,本來(lái)覺(jué)的比較難,原來(lái),真簡(jiǎn)單,個(gè)位數(shù)的代碼就搞定。

[[119979]] 

用Linux中的shell獲取天氣,本來(lái)覺(jué)的比較難,原來(lái),真簡(jiǎn)單,個(gè)位數(shù)的代碼就搞定。

1獲取對(duì)應(yīng)城市天氣

所有天氣信息都從中國(guó)天氣網(wǎng)獲取。每一個(gè)城市多會(huì)對(duì)應(yīng)一個(gè)id(比如,北京為101010100,因?yàn)楸救嗽阢y川,所以例子中就用銀川的id:101170101),通過(guò)id就可以獲取對(duì)應(yīng)城市實(shí)時(shí)天氣或者全天天氣,還可以獲取七天天氣。

1.1shell腳本

shell腳本代碼如下:

  1. #!/bin/sh   
  2. weatherDateRoot=http://www.weather.com.cn/data/sk/101170101.html  
  3. weatherweatherDataFile=weather.html  
  4. wget $weatherDateRoot -O $weatherDataFile > /dev/null 2>&1   
  5. sed 's/.*temp":"\([0-9]\{1,2\}\).*/\1/g' $weatherDataFile 

此腳本通過(guò)將天氣信息獲取,然后通過(guò)正則匹配到當(dāng)前溫度。

如果你只用這個(gè)腳本,不再進(jìn)行二次處理,那也太麻煩。我獲取天氣信息后是顯示到終端命令提示符中的,所以需要還要在做處理。

2終端命令提示符中顯示天氣

首先獲取對(duì)應(yīng)城市天氣,如銀川對(duì)應(yīng)的實(shí)時(shí)天氣信息在:

http://www.weather.com.cn/data/sk/101170101.html

你先在中國(guó)天氣網(wǎng)搜索到你想要的城市的天氣,網(wǎng)址中會(huì)包含城市天氣id,將上面的網(wǎng)址中的id替換成你城市的id就可以獲取。

還有全天天氣信息:

http://www.weather.com.cn/data/cityinfo/101170101.html

不知道中國(guó)天氣網(wǎng)提供七天天氣信息沒(méi)有?如果有,那么我們也可以通過(guò)此方法獲取七天天氣信息。

2.1獲取天氣信息

對(duì)應(yīng)shell腳本:

  1. #!/bin/sh  
  2. allDataUrl=http://www.weather.com.cn/data/cityinfo/101170101.html  
  3. allDataFile=/home/snowsolf/shell/weather/allDay.html  
  4. dataUrl=http://www.weather.com.cn/data/sk/101170101.html  
  5. dataFile=/home/snowsolf/shell/weather/weather.html  
  6. wget $dataUrl -O $dataFile > /dev/null 2>&1  
  7. wget $allDataUrl -O $allDataFile > /dev/null 2>&1 

 2.2定時(shí)獲取

通過(guò)crontab命令設(shè)置定時(shí)任務(wù),執(zhí)行crontab -e命令(如果***次需要設(shè)置默認(rèn)編輯器),然后在文件末尾添加:

  1. */30 * * * * /home/snowsolf/shell/weather/weather.sh >> /dev/null 

此行代碼設(shè)置每30分鐘執(zhí)行一次獲取天氣的腳本,具體crontab命令其它語(yǔ)法可以google或baidu。

2.3提取天氣

  1. sed 's/.*temp":"\([0-9]\{1,2\}\).*/\1/g' 

此命令可以從獲取的實(shí)時(shí)天氣文件中獲取實(shí)時(shí)天氣。

2.4終端命令提示符中顯示

你可以參考http://www.cnblogs.com/snowsolf/p/3192224.html。這里可以讓你的命令提示符更絢麗。

***上一幅我的命令提示符圖:

我的終端命令提示符

簡(jiǎn)直酷斃了!??!做一個(gè)愛(ài)折騰的程序員真好!

原文鏈接:http://www.cnblogs.com/snowsolf/p/shell_get_weather.html

責(zé)任編輯:牛小雨 來(lái)源: snowsolf的博客
相關(guān)推薦

2018-01-29 11:25:37

LinuxASCII 字符天氣預(yù)報(bào)

2019-07-29 15:36:15

Linuxshell內(nèi)置命令

2024-11-27 09:19:25

2009-07-07 09:25:08

Linux開(kāi)發(fā)FOSS開(kāi)發(fā)項(xiàng)目

2009-12-25 09:55:18

LinuxShell編程Shell基本語(yǔ)法

2020-02-11 20:00:29

開(kāi)源開(kāi)源工具天氣預(yù)報(bào)

2009-10-23 13:44:03

linux Shell

2011-01-07 17:33:47

linuxshell

2011-01-10 09:54:30

linuxshell

2019-12-01 22:59:43

Linux shell命令進(jìn)程

2009-10-23 13:24:20

linux Shell

2009-12-25 09:49:32

LinuxShell編程運(yùn)行Shell程序

2012-01-18 10:46:33

ibmdw

2009-12-10 17:02:49

Linux shell

2009-08-28 10:51:21

linux操作系統(tǒng)linux學(xué)習(xí)SHELL

2010-06-23 16:35:50

Linux Bash

2010-06-23 17:37:14

Linux Bash

2019-10-24 07:57:37

Linuxshell獲取時(shí)間

2022-08-25 21:46:51

網(wǎng)絡(luò)通訊應(yīng)用開(kāi)發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)