import urllib.request
def get_weather(localcode): 
    url="http://www.weather.com.cn/weather/"+str(localcode)+".shtml"
    req=urllib.request.Request(url)
    f_str=urllib.request.urlopen(req).read().decode('utf-8')
    f1=str(f_str)
    f_on=f1.find("fc_24h_internal_update_time")
    f_off=f1.find("（明天）")
    f_today=f1[f_on:f_off]
    w1=f_today.find("wea")+5 #start position of today's weather
    w2=f_today.find("tem")-15 #end positon of today's weather
    f_tdw=f_today[w1:w2]
    res=[]
    res.append(f_tdw)
    t1=f_today.find("tem")+7 #start position of today's temperature
    t2=f_today.find("win")-20 #end positon of today's temperature
    f_tdt=f_today[t1:t2]
    ctrl_1=f_tdt.find("</span>/<i>")
    if ctrl_1==-1:
        ctrl_1=f_tdt.find("i>")+2
        ctrl_2=f_tdt.find("℃")
        temp=f_tdt[ctrl_1:ctrl_2]+ " C "
    else:
        ctrl_2=ctrl_1+11
        tem_h=f_tdt[5:ctrl_1] #high temperature
        tem_l=f_tdt[ctrl_2:-1] #low tempterature
        temp=tem_l+"/"+tem_h +" C "
    res.append(temp)
    return res

if __name__=="__main__":    
    print("+-----------------------------------+")
    print("|          Weather Get V1.5         |")
    print("|   modified by zwy at Nov21,2020   |")
    print("+-----------------------------------+")
    print("Get today's weather from http://www.weather.com.cn\n")
    weathermartrix=[
    ["哈尔滨",101050101,""],
    ["呼和浩特",101080101,""],
    ["乌鲁木齐",101130101,""],
    ["北京",101010100,""],
    ["天津","101030100",""],
    ["兰州",101160101,""],
    ["淮南",101220401,""],
    ["上海",101020100,""],
    ["广州",101280101,""],
    ["海口",101310101,""],
    ]
    print("{:_^44}".format(""))
    tplt="{0:{3}^7}|{1:{3}^8}|{2:^12}"
    print(tplt.format("地区","天气","temperature",chr(12288)))
    print("{:-^44}".format(""))
    for i in range(0,len(weathermartrix)):
        weathermartrix[i][2]=get_weather(weathermartrix[i][1])
        print(tplt.format(weathermartrix[i][0],weathermartrix[i][2][0],weathermartrix[i][2][1],chr(12288)))
    print("{:=^44}".format(""),end="\n\n")
    
    

