Call Wanwei Yiyuan API to realize weather forecast

1. Introduction of the author

Fang Gengchen, male, 22nd graduate student, School of Electronic Information, Xi'an Polytechnic University
Research direction: Machine Vision and Artificial Intelligence
Email: [email protected]

Zhang Siyi, female, School of Electronic Information, Xi'an Polytechnic University, 2022 graduate student, Zhang Hongwei Artificial Intelligence Research Group
Research direction: machine vision and artificial intelligence
Email: [email protected]

2. Introduction to theoretical knowledge

2.1 Introduction to Weather Forecasting

Weather forecasting is the application of the laws of atmospheric changes, based on the current and recent weather conditions, to predict the weather conditions in a certain period of time in the future.
Weather forecasting plays a huge role in many aspects, such as: drought detection, agricultural and production planning, aviation industry and personal travel, etc.

2.2 Prediction method

(1) Synoptic forecasting method (or called weather map method):
it uses the weather map as the main tool, cooperates with satellite cloud images, radar maps, etc., uses synoptic principles to analyze and study the changing laws of the weather, so as to make weather forecasts method. This method is mainly used for making short-term forecasts.
(2) Numerical forecasting method (also known as dynamic forecasting method):
It is a method of making weather forecast by using a large and fast electronic computer to solve the dynamic equations describing atmospheric motion. This method can be used to make short-term forecasts, as well as medium and long-term forecasts. In recent years, it has also begun to be used for climate forecasting.
(3) Statistical forecast method:
using a large number of long-term meteorological observation data, according to the principle of probability and statistics, to find out the statistical law of weather changes, and to establish a statistical model of weather changes to make weather forecasts. This method is mainly used to make medium and long-term forecasts and forecasts of meteorological elements.

3. Code implementation

3.1 Packages that need to be installed

python3.6.5
needs to introduce the requests package: run terminal -> enter python/Scripts -> input: pip install requests

3.2 Part of the code

from ShowapiRequest import ShowapiRequest
r=ShowapiRequest("http://route.showapi.com/9-9","1437460","04e4f362e2ae447eb22e7eedcdd0d799" )  #查询网址和appid和secret
r.addBodyPara("areaCode","")
r.addBodyPara("area","西安")  #查询地址
res = r.post()
print(res.text) # 返回信息

子函数ShowapiRequest代码 
import requests
from urllib import parse
#全局请求头
files = {
    
    }
headers = {
    
    }
body = {
    
    }
timeouts = {
    
    }
resHeader = {
    
    }

class ShowapiRequest:
    def __init__(self, url, my_appId, my_appSecret):
        self.url = url
        self.my_appId = my_appId
        self.my_appSecret = my_appSecret
        body["showapi_appid"] = my_appId
        body["showapi_sign"] = my_appSecret
        headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2427.7 Safari/537.36"

    def addFilePara(self, key, value_url):
        files[key] = open(r"%s "% (value_url), 'rb')
        return self

    def addHeadPara(self, key, value):
        headers[key] = value
        return self

    def addBodyPara(self, key, value):
        body[key] = value
        return self
    #设置连接时间和读取时间
    def setTimeout(self, connecttimout, readtimeout):
        timeouts["connecttimout"] = connecttimout
        timeouts["readtimeout"] = readtimeout
        return self


    def get(self):
        get_url = self.url + "?" + parse.urlencode(body)
        if not timeouts:
            res = requests.get(get_url, headers=headers)
        else:
            timeout = (timeouts["connecttimout"], timeouts["readtimeout"])
            res = requests.get(get_url, headers=headers, timeout=timeouts)
        return res

    def post(self):
        if not timeouts:
            res = requests.post(self.url, files=files, data=body, headers=headers)
        else:
            timeout = (timeouts["connecttimout"], timeouts["readtimeout"])
            res = requests.post(self.url, files=files, data=body, headers=headers, timeout=timeout)
        return res



3.3 Experimental results

insert image description here

insert image description here
Experimental results show the current date, address, and weather conditions.

Guess you like

Origin blog.csdn.net/m0_37758063/article/details/131294635