Pyhton3 uses the request module and the requests module to implement weather forecast query

API interface of China Weather Network http://www.weather.com.cn/data/sk/ city code.html

The city code of Guangzhou is 101280101 The city code of Shaoguan is 101280201

The API interface of China Weather Network Guangzhou Weather is  http://www.weather.com.cn/data/sk/101280101.html

code show as below:

#!/usr/bin/env python3
from urllib import request
import json

city_web = 'http://www.weather.com.cn/data/sk/101280101.html'
r = request.urlopen (city_web) ## Using the request module, put the request result in r
data = r.read ( ) ## Read the data in r and assign it to data, this is a string in json format
print (json.loads (data)) ## Use the json module, which can be understood here as a string in json format , Converted to a dictionary
r.close ()

The results obtained are as follows:

{'weatherinfo': {'city': 'Guangzhou', 'cityid': '101280101', 'temp': '26 .6 ',' WD ':' Southeast Wind ',' WS ':' less than level 3 ',' SD ': '83%', 'AP': '1001.4hPa', 'njd': 'Not yet live', 'WSE': '<3', 'time': '17: 50 ',' sm ': '1.7', 'isRadar': '1', 'Radar': 'JC_RADAR_AZ9200_JB'}}

See here, the result is a dictionary.

Expand the script. Run a script that allows users to choose to query the weather in different cities

The script is as follows:

#! / usr / bin / env python3
from urllib import request
import json
def get_info (city_code): 
    city_web = 'http://www.weather.com.cn/data/sk/%s.html'% city_code #Incoming The variable generates the corresponding url
    r = request.urlopen (city_web) #Get a json format data
    data = r.read () #Read the json format data and assign it to data
    data = json.loads (data) #Use json Module to convert data into characters recognized by Python
    output = 'Wind direction:% s, Wind force:% s, Temperature:% s, Humidity:% s'% (
        data [' weatherinfo '] [' WD '],
        data [' weatherinfo '] [' WS '],
        data['weatherinfo']['temp'],
        data ['weatherinfo'] ['SD']
    )
    return output
if __name__ == '__main__':
    city_code = {0: '101280101', 1: '101280201'} ## Define a dictionary and store the city code
    prompt = '' '(0) Query the weather conditions in Guangzhou ## Define a display bar
(1) Query the weather conditions in Shaoguan
Please select the function you want, enter 0/1:' ''
    choice = int (input (prompt)) #User input The result is converted to a number
    print (get_info (city_code [choice])) #When choice is 0, it is equivalent to get_info (101280101), pass a variable to the function

 

 

Use the requests module to achieve the above functions, the code is as follows:

#!/usr/bin/env python3
import requests

def get_info(city_code):
    city_web = 'http://www.weather.com.cn/data/sk/%s.html' % city_code
    r = requests.get(city_web)
    r.encoding = 'utf8'
    data = r.json()
    output = '风向:%s, 风力: %s, 温度:%s, 湿度:%s' % (
        data['weatherinfo']['WD'],
        data['weatherinfo']['WS'],
        data['weatherinfo']['temp'],
        data['weatherinfo']['SD']
    )
    return output

if __name__ == '__main__':
    city_code = {0: '101280101', 1: '101280201'}
    prompt = '' '(0) query the weather conditions in Guangzhou
(1) query the weather conditions in Shaoguan,
please select the one you want to query Region, choose 0/1: '' '
    choice = int (input (prompt))
    print (get_info (city_code [choice]))

Published 73 original articles · praised 4 · 20,000+ views

Guess you like

Origin blog.csdn.net/qq_27592485/article/details/102479334