1、编写源代码 - 查询城市天气.
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
# 功能:查询城市天气
import requests, json, re
from matplotlib import pyplot as plt
# 获取城市代码
def getCityCode(city):
url = 'http://toy1.weather.com.cn/search?cityname=' + city
r = requests.get(url)
if len(r.text) > 4:
json_arr = json.loads(r.text[1:len(r.text)-1])
code = json_arr[0]['ref'][0:9]
return code
else:
return "000000000"
# 获取城市天气信息
def getWeatherInfo(city):
code = getCityCode(city)
url = 'http://t.weather.sojson.com/api/weather/city/' + code
r = requests.get(url)
info = r.json()
weather = {}
if info['status'