這是一個經過python中TkInter,Requests,Re模塊實現的天氣預報爬蟲程序,軟件界面爲圖形界面,軟件運行界面以下:html
操做方法只須要在查詢城市編輯框輸入完整的城市名稱或完整拼音字母,好比深圳能夠輸入shenzhen,而後點查詢就可查詢,目前程序實現了國內和國際的天氣預報查詢,國內8日內天氣預報查詢,國外實現7日內天氣預報查詢功能,java
同時支持軟件開啓自動顯示當前城市天氣預報查詢功能:python
源代碼以下:c++
1 # *_* coding:utf-8 *_* 2 3 # 開發團隊:中國軟件開發團隊 4 # 開發人員:Administrator 5 # 開發時間:2019/3/23 5:16 6 # 文件名稱:weatherSpider 7 # 開發工具:PyCharm 8 9 10 import tkinter 11 import tkinter.messagebox 12 from tkinter import ttk 13 import requests 14 # from PIL import ImageTk as itk 15 from selenium import webdriver 16 # from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 17 from selenium.webdriver.chrome.options import Options 18 import re 19 20 21 ''' 22 獲取本地所在城市名稱 23 ''' 24 25 26 def get_local_city(): 27 chrome_options = Options() 28 chrome_options.add_argument('--headless') 29 chrome_options.add_argument('--disable-gpu') 30 # 31 # 更換頭部 32 chrome_options.add_argument( 33 'user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"') 34 35 driver = webdriver.Chrome(executable_path='./chromedriver.exe', chrome_options=chrome_options) 36 driver.get("http://www.weather.com.cn") 37 text = driver.page_source 38 39 result = re.findall('<span class="city_name"><em>(.*?)</em></span>', text, re.S) 40 driver.close() 41 return result[0] 42 43 44 class MyFrame(tkinter.Frame): 45 def __init__(self, default_city): 46 self.root = tkinter.Tk() 47 48 self.root.title("天氣查詢") 49 self.root.geometry('1200x700+400+220') 50 # 修改默認應用程序圖標 51 self.root.iconbitmap('camero.ico') 52 53 bg = tkinter.Canvas(self.root, width=1200, height=600, bg='white') 54 # self.img = itk.PhotoImage(file="bg.jpg") 55 bg.place(x=100, y=40) 56 # bg.create_image(0, 0, anchor=tkinter.NW, image=self.img) 57 58 self.city = tkinter.Entry(self.root, width=16, font=("仿宋", 18, "normal")) 59 self.city.place(x=200, y=60) 60 self.city.insert(0, default_city) 61 62 citylabel = tkinter.Label(self.root, text='查詢城市', font=("仿宋", 18, "normal")) 63 citylabel.place(x=80, y=60) 64 65 # 查詢按鈕 66 chaxun = tkinter.Button(self.root, width=10, height=3, text="查詢", bg='#00CCFF', bd=5, font="bold",command=self.search) 67 68 chaxun.place(x=800, y=50) 69 70 # 清除按鈕 71 clearbtn = tkinter.Button(self.root, width=10, height=3, text="清除", bg='#00CCFF', bd=5, font="bold",command=self.clear) 72 73 clearbtn.place(x=950, y=50) 74 75 poslabel = tkinter.Label(self.root, text='選擇位置', font=("仿宋", 18, "normal")) 76 poslabel.place(x=80, y=100) 77 78 comvalue = tkinter.StringVar() # 窗體自帶的文本,新建一個值 79 self.comboxlist = ttk.Combobox(self.root, width=30, height=18, font=("仿宋", 18, "normal"), 80 textvariable=comvalue) # 初始化 81 self.comboxlist["values"] = ("1", "2", "3") 82 self.comboxlist.current(0) # 選擇第一個 83 self.comboxlist.bind("<<ComboboxSelected>>", self.choose) # 綁定事件,(下拉列表框被選中時,綁定choose()函數) 84 self.comboxlist.place(x=200, y=100) 85 86 self.result = tkinter.Listbox(self.root, heigh=18, width=65, font=("仿宋", 20, "normal")) # 顯示天氣框 87 self.result.place(x=125, y=150) 88 89 self.citys = [] 90 91 self.headers = { 92 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36', 93 'Cookie': '__guid=182823328.3322839646442213000.1543932524694.901; vjuids=1858d43b6.167798cbdb7.0.8c4d7463d5c5d; vjlast=1543932526.1543932526.30; userNewsPort0=1; f_city=%E5%B9%B3%E9%A1%B6%E5%B1%B1%7C101180501%7C; Hm_lvt_080dabacb001ad3dc8b9b9049b36d43b=1543932526,1543932551,1543932579; Wa_lvt_1=1547464114,1547464115,1547880054,1547983123; defaultCty=101181001; defaultCtyName=%u5546%u4E18; monitor_count=6; Wa_lpvt_1=1547983809' 94 } 95 96 # 開啓本地天氣查詢 97 if (default_city != ''): 98 self.tianqiforecast(default_city) 99 100 def tianqiforecast(self, searchcity): 101 102 city = searchcity 103 url = 'http://toy1.weather.com.cn/search?cityname=' + city + '&callback=success_jsonpCallback&_=1548048506469' 104 response = requests.get(url, headers=self.headers) 105 html1 = response.content.decode('utf-8') 106 self.citys = re.findall('"ref":"(.*?)~.*?~(.*?)~.*?~(.*?)~.*?~.*?~.*?~.*?~(.*?)"', html1, re.S) 107 if (len(self.citys) == 0): 108 a = "出錯了,未查找到該城市" 109 self.result.insert(tkinter.END, a) 110 return 111 # 顯示當前城市經常使用查詢點 112 plist = [] 113 for i in range(0, len(self.citys)): 114 # print(i + 1, ':%14s ' % "".join(citys[i])) 115 plist.append(self.citys[i][1]) 116 pos = tuple(plist) 117 self.comboxlist["values"] = pos 118 self.comboxlist.current(0) 119 if len(self.citys) != 0: 120 self.query(0) 121 122 def search(self): 123 mycity = self.city.get() 124 if (mycity != ''): 125 self.clear() 126 self.tianqiforecast(mycity) 127 128 def query(self, choose): 129 if (len(self.citys[choose][0]) == 9): 130 if (self.citys[choose][0][0] != '1' or self.citys[choose][0][1] != '0' or self.citys[choose][0][2] != '1'): 131 # 查詢國外天氣 132 133 url2 = 'http://www.weather.com.cn/weathern/' + self.citys[choose][0] + '.shtml' 134 responseweather = requests.get(url2, headers=self.headers) 135 html2 = responseweather.content.decode('utf-8') 136 137 weather = re.findall('<li class="date-.*?".*?".*?">(.*?)</.*?"date-i.*?">(.*?)<.*?', html2, re.S) 138 temp_weather = re.findall( 139 '<p class="weather-info">(.*?)</p>.*?title="(.*?)".*?title="(.*?)".*?<p class="wind-i.*?">(.*?)</p>', 140 html2, re.S) 141 if len(temp_weather) < 7: 142 # 當天 143 today1 = re.findall( 144 '<li class="blue-item active".*?>(.*?)<div class="item-active"></div>\\n</li>', 145 html2, re.S) 146 today = re.findall('<p class="weather-info">(.*?)</p>.*?<p class="wind-info">(.*?)</p>', today1[0], 147 re.S) 148 print(today) 149 # 後6天 150 weather.append(temp_weather) 151 else: 152 weather.append(temp_weather) 153 154 Hightempture = re.findall( 155 '<script>var eventDay =\["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];', html2, 156 re.S) 157 Lowtempture = re.findall( 158 'var eventNight =\["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];', 159 html2, re.S) 160 # print(Hightempture,Lowtempture) 161 b = '查詢城市爲:' + str(self.citys[choose][3]) + ' ' + str(self.citys[choose][1]) 162 self.result.insert(tkinter.END, b) 163 if len(temp_weather) < 7: # 如日本 164 if len(weather) <= 0 or len(Lowtempture) <= 0 or len(Hightempture) <= 0 or \ 165 len(Lowtempture[0]) != 7 or len(Hightempture[0]) != 7: 166 a = '系統出錯,數據不完整:' 167 self.result.insert(tkinter.END, a) 168 self.result.insert(tkinter.END, url2) 169 print(url2) 170 else: 171 for i in range(0, 7): 172 if i < 1: 173 a = "".join(weather[i]) + ' ' + Lowtempture[0][i] + '℃ ~ ' + Hightempture[0][ 174 i] + '℃ ' + str(today[0][0]) + ' 風:' + str(today[0][1]) 175 self.result.insert(tkinter.END, a) 176 177 else: 178 a = "".join(weather[i]) + ' ' + Lowtempture[0][i] + '℃ ~ ' + Hightempture[0][ 179 i] + '℃ ' + "".join(weather[7][i - 1]) 180 181 self.result.insert(tkinter.END, a) 182 else: # 如美國 183 if len(temp_weather) <= 0 or len(Lowtempture) <= 0 or len(Hightempture) <= 0 or \ 184 len(Lowtempture[0]) != 7 or len(Hightempture[0]) != 7: 185 a = '系統出錯,數據不完整:' 186 self.result.insert(tkinter.END, a) 187 self.result.insert(tkinter.END, url2) 188 print(url2) 189 else: 190 for i in range(0, 7): 191 a = "".join(weather[i]) + ' ' + Lowtempture[0][i] + '℃ ~ ' + Hightempture[0][ 192 i] + '℃ ' + "".join(weather[7][i]) 193 194 self.result.insert(tkinter.END, a) 195 196 197 else:#國內天氣查詢 198 url2 = 'http://www.weather.com.cn/weathern/' + self.citys[choose][0] + '.shtml' 199 responseweather = requests.get(url2, headers=self.headers) 200 html2 = responseweather.content.decode('utf-8') 201 202 weather = re.findall('<li class="date-.*?".*?".*?">(.*?)</.*?"date-i.*?">(.*?)<.*?', html2, re.S) 203 weather.append(re.findall( 204 '<p class="weather-in.*?" title="(.*?)".*?title="(.*?)".*?title="(.*?)".*?<p class="wind-i.*?">(.*?)</p>', 205 html2, re.S)) 206 Hightempture = re.findall( 207 '<script>var eventDay =\["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];', html2, 208 re.S) 209 Lowtempture = re.findall( 210 'var eventNight =\["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];', 211 html2, re.S) 212 213 b = '查詢城市爲:' + str(self.citys[choose][3]) + ' ' + str(self.citys[choose][1]) 214 self.result.insert(tkinter.END, b) 215 if len(weather) <= 0 or len(Lowtempture) <= 0 or len(Hightempture) <= 0 or \ 216 len(Lowtempture[0]) != 8 or len(Hightempture[0]) != 8: 217 a = '系統出錯,數據不完整:' 218 self.result.insert(tkinter.END, a) 219 self.result.insert(tkinter.END, url2) 220 print(url2) 221 else: 222 for i in range(0, 8): 223 a = "".join(weather[i]) + ' ' + Lowtempture[0][i] + '℃ ~ ' + Hightempture[0][ 224 i] + '℃ ' + "".join(weather[8][i]) 225 226 self.result.insert(tkinter.END, a) 227 228 if (len(self.citys[choose][0]) == 12): # 查詢搜索相關結果的下一個城市天氣預報 229 url2 = 'http://forecast.weather.com.cn/town/weathern/' + self.citys[choose][0] + '.shtml' 230 responseweather = requests.get(url2, headers=self.headers) 231 html2 = responseweather.content.decode('utf-8') 232 233 weather = re.findall('<li class="date-.*?".*?"da.*?">(.*?)</.*?"date-i.*?">(.*?)<.*?', html2, re.S) 234 235 html2 = re.sub('lt;', '<', html2) 236 weather.append(re.findall( 237 '<p class="weather-in.*?" title="(.*?)".*?title="(.*?)".*?title="(.*?)".*?<p class="wind-i.*?">\\r\\n(.*?)\\r\\n', 238 html2, re.S)) 239 240 Hightempture = re.findall( 241 'var eventDay = \["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];', html2, re.S) 242 243 Lowtempture = re.findall( 244 'var eventNight = \["(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)","(.*?)"\];', 245 html2, re.S) 246 # print(Hightempture,Lowtempture) 247 b = '查詢城市爲:' + str(self.citys[choose][3]) + ' ' + str(self.citys[choose][2]) + ' ' + str( 248 self.citys[choose][1]) 249 self.result.insert(tkinter.END, b) 250 251 if len(weather) <= 0 or len(Lowtempture) <= 0 or len(Hightempture) <= 0 or \ 252 len(Lowtempture[0]) != 8 or len(Hightempture[0]) != 8: 253 a = '系統出錯,數據不完整:' 254 self.result.insert(tkinter.END, a) 255 self.result.insert(tkinter.END, url2) 256 print(url2) 257 else: 258 for i in range(0, 8): 259 a = "".join(weather[i]) + ' ' + Lowtempture[0][i] + '℃ ~ ' + Hightempture[0][ 260 i] + '℃ ' + "".join(weather[8][i]) 261 # print(a) 262 self.result.insert(tkinter.END, a) 263 264 ''' 265 選擇搜索城市相關的下一個城市名稱,並進行天氣查詢 266 ''' 267 268 def choose(self, event): 269 c = self.comboxlist.get() 270 choose = -1 271 for i in range(0, len(self.citys)): 272 if c == self.citys[i][1]: 273 choose = i; 274 break; 275 if choose != -1: 276 self.query(choose) 280 281 ''' 282 清除天氣查詢結果 283 ''' 284 285 def clear(self): 286 self.result.delete(0, tkinter.END) 287 # self.city.delete(0, tkinter.END) 288 # tkinter.messagebox.showerror('showerror', 'hello') 289 290 291 292 if __name__ == '__main__': 293 # 獲取當前城市 294 default_city = get_local_city(); 295 296 myframe = MyFrame(default_city) 297 myframe.root.mainloop()
經過該程序主要學習爬蟲的解決問題思想和熟悉規則表達式Re模塊,網絡數據抓取requests模塊,selenium模塊及TkInter GUI模塊的具體使用方法。web
對於初學python網友能夠參考一下幾本優秀的圖書算法
零基礎學python 配套光盤下載地址chrome
其它有關爬蟲開發參考資料可參考以下書籍:json
查看目錄網絡
<a href="https://itdocs.pipipan.com/fs/3843664-360760144" target="_blank">Python數據抓取技術與實戰.pdf</a><br/>
<a href="https://itdocs.pipipan.com/fs/3843664-360759832" target="_blank">Python爬蟲入門到實戰.pdf</a><br/>
<a href="https://itdocs.pipipan.com/fs/3843664-360759804" target="_blank">Python爬蟲實戰入門教程.pdf</a><br/>
<a href="https://itdocs.pipipan.com/fs/3843664-360759781" target="_blank">Python網絡數據採集.pdf</a><br/>
<a href="https://itdocs.pipipan.com/fs/3843664-360759778" target="_blank">PYTHON網絡爬蟲從入門到實踐.pdf</a><br/>
<a href="https://itdocs.pipipan.com/fs/3843664-360759784" target="_blank">《Python爬蟲開發與項目實戰》.pdf</a><br/>
<a href="https://itdocs.pipipan.com/fs/3843664-360759859" target="_blank">精通Scrapy網絡爬蟲.pdf</a><br/>
<a href="https://itdocs.pipipan.com/fs/3843664-360759787" target="_blank">網絡爬蟲-Python和數據分析.pdf</a><br/>
<a href="https://itdocs.pipipan.com/fs/3843664-360759790" target="_blank">網絡爬蟲全解析 技術、原理與實踐.pdf</a><br/>
<a href="https://itdocs.pipipan.com/dir/3843664-33687271-44035c/" target="_blank">Python 3爬蟲、數據清洗與可視化實戰</a><br/>