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

Python命令行實現(xiàn)—查全國7天天氣

開發(fā) 后端
為什么要爬天氣呢?1.可以練練手2.利用itchat庫實現(xiàn)自動回復(fù)功能后,把查天氣的功能集成起來,實現(xiàn)微信自助查天氣功能!

為什么要爬天氣呢?1.可以練練手2.利用itchat庫實現(xiàn)自動回復(fù)功能后,把查天氣的功能集成起來,實現(xiàn)微信自助查天氣功能!

首先,還是相似的套路,我們看看能不能在官網(wǎng)上直接抓包(XHR)來獲取一個通用API。然后直接用API查詢就OK?在百度搜關(guān)鍵詞【天氣】或者【南京天氣】會跳出對應(yīng)的網(wǎng)頁:http://www.weather.com.cn/weather/101190101.shtml.點進(jìn)去,可以看到相應(yīng)城市下一周的天氣情況:

 

Python命令行實現(xiàn)—查全國7天天氣

再換一個城市上海,我們發(fā)現(xiàn),瀏覽器地址變?yōu)椋篽ttp://www.weather.com.cn/weather/101020100.shtml。原來101020100這串?dāng)?shù)字對應(yīng)著相應(yīng)城市的代碼。我們來分析下頁面上XHR請求,看看有沒有直接抓包的可能?

經(jīng)過谷歌瀏覽器——檢查-Networt-XHR-刷新,發(fā)現(xiàn)并沒有XHR請求,看來我們需要的天氣內(nèi)容和城市代碼,可能是包含在頁面中經(jīng)過JS和服務(wù)器處理后呈現(xiàn)的.....好吧,嘗試失敗!

再看一下JS請求,發(fā)現(xiàn)太多了,無力去逐一查看!所幸網(wǎng)上有人早已記錄下了所有城市對應(yīng)的城市代碼。我把拷貝了一下,存到了本地mysql,數(shù)據(jù)在百度云上,需要的可以自行下載下,執(zhí)行SQL即可直接把SQL表和數(shù)據(jù)一并建好。https://pan.baidu.com/s/1kXaN2Aj 密碼是:8y6n。

好了,準(zhǔn)備工作做完了,現(xiàn)在思路就很清楚了,全國城市和代碼都有了,我們查某個城市的天氣,只需要輸入城市,就可以從mysql里獲取對應(yīng)的城市代碼如:101020100,然后構(gòu)造相應(yīng)的url:http://www.weather.com.cn/weather/101190101.shtml就可以查看到對應(yīng)城市的7天天氣了,然而,頁面并沒有XHR和直接可用的json數(shù)據(jù),那我們只能自己動手了——分析網(wǎng)頁內(nèi)容,動手寫正則表達(dá)式/beautifulSoup/Xpath來提取頁面信息,具體內(nèi)容在此就不贅述了,詳見代碼就好:

  1. import re 
  2. import pymysql 
  3. import requests 
  4. from bs4 import BeautifulSoup 
  5.  
  6. class SearchWeather(): 
  7.     def __init__(self): 
  8.         self.HEADERS ={ 
  9.         'User-Agent''Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 ''(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
  10.         self.CONNECTION = pymysql.connect(host='localhost',user='root',password='xxx',db='xxx',charset='utf8',cursorclass=pymysql.cursors.DictCursor) 
  11.  
  12.     def getcityCode(self,cityName): 
  13.         SQL = "SELECT cityCode FROM cityWeather WHERE cityName='%s'" % cityName 
  14.         try: 
  15.             with self.CONNECTION.cursor() as cursor
  16.                 cursor.execute(SQL) 
  17.                 self.CONNECTION.commit() 
  18.                 result = cursor.fetchone() 
  19.                 return result['cityCode'
  20.         except Exception as e: 
  21.             print(repr(e)) 
  22.  
  23.     def getWeather(self,cityCode,cityname): 
  24.         url = 'http://www.weather.com.cn/weather/%s.shtml' % cityCode 
  25.         html = requests.get(url,headers = self.HEADERS) 
  26.         html.encoding='utf-8' 
  27.         soup=BeautifulSoup(html.text,'lxml'
  28.         weather = "日期      天氣    【溫度】    風(fēng)向風(fēng)力\n" 
  29.         for item in soup.find("div", {'id''7d'}).find('ul').find_all('li'): 
  30.             date,detail = item.find('h1').string, item.find_all('p'
  31.             title = detail[0].string 
  32.             templow = detail[1].find("i").string 
  33.             temphigh = detail[1].find('span').string if detail[1].find('span')  else '' 
  34.             wind,direction = detail[2].find('span')['title'], detail[2].find('i').string 
  35.             if temphigh==''
  36.                 weather += '你好,【%s】今天白天:【%s】,溫度:【%s】,%s:【%s】\n' % (cityname,title,templow,wind,direction) 
  37.             else
  38.                 weather += (date + title + "【" + templow +  "~"+temphigh +'°C】' + wind + direction + "\n"
  39.         return weather 
  40.  
  41.     def main(self,city): 
  42.         cityCode = self.getcityCode(city) 
  43.         detail = self.getWeather(cityCode,city) 
  44.         print (detail) 
  45.  
  46. if __name__ == "__main__"
  47.     weather = SearchWeather() 
  48.     weather.main(city=input('請輸入城市名稱:')) 

代碼運行效果如下:

 

Python命令行實現(xiàn)—查全國7天天氣

 

責(zé)任編輯:未麗燕 來源: 今日頭條
相關(guān)推薦

2020-12-10 16:16:08

工具代碼開發(fā)

2020-12-11 06:44:16

命令行工具開發(fā)

2015-10-19 17:16:10

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

2010-10-12 17:13:43

MySQL命令行

2010-03-10 17:23:37

Python 命令行參

2010-11-16 11:50:21

oracle命令行登錄

2021-07-07 08:01:51

命令行Dotnet Core控制臺

2010-11-16 11:55:31

Oracle命令行

2015-07-29 10:34:50

Linux系統(tǒng)命令行工具

2015-07-30 11:04:08

Linux命令行工具

2015-07-30 11:24:47

Linux 系統(tǒng)命令行工具

2015-07-01 09:15:46

linuxQuora命令行

2010-03-11 15:55:23

Python命令行

2018-05-04 09:15:35

PythonPlumbum命令行

2010-11-24 14:51:55

Mysql命令行

2010-02-23 16:06:58

Python 命令行

2018-07-05 08:30:54

Python命令行工具shell

2010-09-01 14:23:54

Linux命令行開發(fā)

2010-11-24 15:33:59

mysql命令行參數(shù)

2019-07-23 13:45:38

LinuxFedora權(quán)限
點贊
收藏

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