1. 程式人生 > >record-11 網絡編程 打開文件

record-11 網絡編程 打開文件

day adl eat div data pytho lines for parse

#open()
#urlopen()
#方法名稱不同
#參數不同
#只能以只讀模式打開網絡資源文件

from urllib.request import urlopen
from urllib.parse import quote
from json import loads
from time import time,sleep

print(‘請輸入要查詢的城市名稱:‘)
s=input()
s1=quote(s)
#print(s1)


url=‘http://www.sojson.com/open/api/weather/json.shtml?city=‘+s1
f=urlopen(url)
#read()
#readline()
#readlines()
result=f.read().decode()
result=loads(result) #a=a+1

day1=result[‘data‘][‘forecast‘][0]
print(‘日期:‘,day1[‘date‘])
print(‘天氣狀態:‘,day1[‘type‘])
print(‘風向:‘,day1[‘fx‘])
print(‘今天最低溫度:‘,day1[‘low‘])
print(‘今天最高溫度:‘,day1[‘high‘])
print(‘註意事項:‘,day1[‘notice‘])


f.close()

  

record-11 網絡編程 打開文件