偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Linux-shell獲取天氣

系統(tǒng) Linux
用Linux中的shell獲取天氣,本來覺的比較難,原來,真簡單,個位數(shù)的代碼就搞定。

[[119979]] 

用Linux中的shell獲取天氣,本來覺的比較難,原來,真簡單,個位數(shù)的代碼就搞定。

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

所有天氣信息都從中國天氣網(wǎng)獲取。每一個城市多會對應(yīng)一個id(比如,北京為101010100,因為本人在銀川,所以例子中就用銀川的id:101170101),通過id就可以獲取對應(yīng)城市實時天氣或者全天天氣,還可以獲取七天天氣。

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 

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

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

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

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

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

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

還有全天天氣信息:

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

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

2.1獲取天氣信息

對應(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定時獲取

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

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

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

2.3提取天氣

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

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

2.4終端命令提示符中顯示

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

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

我的終端命令提示符

簡直酷斃了?。?!做一個愛折騰的程序員真好!

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

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

2018-01-29 11:25:37

LinuxASCII 字符天氣預(yù)報

2019-07-29 15:36:15

Linuxshell內(nèi)置命令

2024-11-27 09:19:25

2020-02-11 20:00:29

開源開源工具天氣預(yù)報

2009-07-07 09:25:08

Linux開發(fā)FOSS開發(fā)項目

2009-10-23 13:44:03

linux Shell

2009-12-25 09:55:18

LinuxShell編程Shell基本語法

2011-01-07 17:33:47

linuxshell

2019-12-01 22:59:43

Linux shell命令進(jìn)程

2011-01-10 09:54:30

linuxshell

2009-12-25 09:49:32

LinuxShell編程運行Shell程序

2009-10-23 13:24:20

linux Shell

2022-08-25 21:46:51

網(wǎng)絡(luò)通訊應(yīng)用開發(fā)

2015-10-19 17:16:10

天氣預(yù)報命令行Linux

2019-10-24 07:57:37

Linuxshell獲取時間

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

2012-01-18 10:46:33

ibmdw
點贊
收藏

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