基於Python的簡單天氣爬蟲程式

mei1092670355發表於2018-03-26
import re
import urllib.request
import tkinter
from tkinter import *
import matplotlib.pyplot as plt
from pylab import *  
def zuoye():
    url="http://tianqi.so.com/weather/"
    data=urllib.request.urlopen(url).read().decode()
    
    pat0='<div class="city-change-wrap">(.*?)</div>'
    result0=re.compile(pat0).findall(data)
    result0=result0[0]
    pat01='<strong class="change-title">(.*?)</strong>'
    result01=re.compile(pat01).findall(result0)#地址資訊
    
    pat1='<div class="cur-weather g-fl">(.*?)</div>'
    result1=re.compile(pat1).findall(data)
    result1=result1[0]
    pat2='</span>(.*?)</p>'
    pat3='<span>(.*?)</span>'
    result2=re.compile(pat2).findall(result1)#(天氣,體感,風向,分力,氣壓,溼度)
    result3=re.compile(pat3).findall(result1)#天氣資訊所對應的值
    
    pat4='<div class="badthing-item g-fl ">(.*?)</div>'
    result4=re.compile(pat4).findall(data)
    result4=result4[0]
    pat5='<span>(.*?)</span>'
    pat6='<span class="g-fl">(.*?)</span>'
    result5=re.compile(pat5).findall(result4)#pm2.5
    result6=re.compile(pat6).findall(result4)#pm2.5值


    pat8='<div class="mh-detail-tips">(.*?)</div>'
    result8=re.compile(pat8).findall(data)#出行建議資訊


    pat9='<div class="local-time">(.*?)</div>'
    result9=re.compile(pat9).findall(data)
    result9=result9[0]
    result10=re.compile(pat5).findall(result9)#爬取時間資訊
    
    pat16='</div><div class="curve-info-item" style="width: 12.5%"><p>(.*?)</p>'
    pat17='11時'
    pat18='<div class="weather-trend">(.*?)</div>'
    result11=re.compile(pat16).findall(data)
    result11=str(result11)
    a=result11.replace("時", "")
    a=a.replace('\'',r'')
    a=a.replace('\]',r'')
    b=[]#時間


    for i in range(1,6):
        c=a.split(',')[i]
        b=b+[c]
    pat23='todayHoursValue(.*?)]'
    result12=re.compile(pat23).findall(data)
    d=[]#溫度
    result12=str(result12)
    result12=result12.replace("=","")
    result12=result12.replace('\'',r'')
    result12=result12.replace('\"',r'')
    result12=result12.replace('\]',r'')
    for i in range(1,6):
        e=result12.split(',')[i]
        d=d+[e]
    def show1():
        y = list(map(int,d))
        x = list(map(int,b))
        for i in range(1,5):
            if x[i]<x[0]:
                x[i]=x[i]+24#使用48小時制,當每個數小於第一個數時那麼他就是第二天,時間+24
        x2 = x
        x1 = range(len(x))
        plt.plot(x, y, marker='*', mec='r', mfc='w',label=u'tempature')
        plt.legend() 
        plt.margins(0)
        plt.subplots_adjust(bottom=0.15)
        plt.xlabel(u"time") 
        plt.ylabel("tempature") 
        plt.title("python") 
        plt.show()#畫折線圖


                
    f=open('test.txt','w')
    f.truncate()#清空檔案
    
    shijian=str(result10)
    shijian="當前時間為:"+shijian
    shijian=shijian.replace('\'',r'')
    dizhi=str(result01)
    dizhi="當前地址為:"+dizhi
    dizhi=dizhi.replace('\'',r'')
    #print(shijian)


    tianqi=dict()#建立字典a用於儲存天氣資訊
    tianqi=dict(zip(result3,result2))
    tianqi1=str(tianqi)
    tianqi1=tianqi1.replace('\'',r'')
    tianqi1=tianqi1.replace(',',r', ')
    #for key,value in tianqi.items():
     #   print(key,value)
    pm=dict()#建立字典b用於儲存pm2.5資訊
    pm=dict(zip(result5,result6))
    pm1=str(pm)
    pm1=pm1.replace('\'',r'')
    jianyi=str(result8)
    jianyi="出行建議:"+jianyi
    jianyi=jianyi.replace('\'',r'')
    f.write(jianyi+'\n')#將建議寫入
    f.write(pm1+'\n')#將pm2.5資訊寫入
    f.write(tianqi1+'\n')#將天氣資訊寫入
    f.write(shijian+'\n')#將時間寫入
    f.write(dizhi+'\n')#將地址寫入
    f.close()
    root=tkinter.Tk()
    root.title('python作業')
    root['height']=800
    root['width']=600
    listboxzuoye=tkinter.Listbox(root,selectmode = BROWSE,width=300)
    listboxzuoye.place(x=0,y=35,width=600,height=770)#設定輸出框
    
    def show():
        f=open('test.txt','r')#讀取檔案內容,逐行顯示
        li=f.readlines()
        for line in li:
            listboxzuoye.insert(0,line)
        
    buttonjisuan1=tkinter.Button(root,text='獲取天氣資訊',width=100,command=show)
    buttonjisuan1.place(x=10,y=10,width=150,height=20)
    buttonjisuan1=tkinter.Button(root,text='檢視溫度折線圖',width=100,command=show1)
    buttonjisuan1.place(x=260,y=10,width=150,height=20)
    
if __name__ == '__main__' :zuoye() 






相關文章