av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

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

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

呼蘭網(wǎng)站建設公司創(chuàng)新互聯(lián)建站,呼蘭網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為呼蘭上千提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務好的呼蘭做網(wǎng)站的公司定做!

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

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

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

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

好了,準備工作做完了,現(xiàn)在思路就很清楚了,全國城市和代碼都有了,我們查某個城市的天氣,只需要輸入城市,就可以從mysql里獲取對應的城市代碼如:101020100,然后構造相應的url:http://www.weather.com.cn/weather/101190101.shtml就可以查看到對應城市的7天天氣了,然而,頁面并沒有XHR和直接可用的json數(shù)據(jù),那我們只能自己動手了——分析網(wǎng)頁內(nèi)容,動手寫正則表達式/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 = "日期      天氣    【溫度】    風向風力\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天天氣
網(wǎng)站URL:http://uogjgqi.cn/article/dpecjdj.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流