requests接口测试-天气预报api多参数

阿里云买的api这俩差不多

https://market.aliyun.com/products/56928004/cmapi014123.html?spm=5176.2020520132.101.5.7f0d7218veuaMm#sku=yuncode812300000

用requests测一下接口

#coding:utf-8
import requests
import json
import base64
class web_requests(object):
    def __init__(self):
        pass
    def Interface(self, area, need3HourForcast, needAlarm, needHourData, needIndex, needMoreDay):
        url = "http://saweather.market.alicloudapi.com/spot-to-weather?area=%s&need3HourForcast=%s&" 
              "needAlarm=%s&needHourData=%s&needIndex=%s&needMoreDay=%s" % 
              (area, need3HourForcast, needAlarm, needHourData, needIndex, needMoreDay)
        headers = {"Host": "saweather.market.alicloudapi.com",
                   "X-Ca-Timestamp": "1542011739255",
                   "gateway_channel": "http",
                   "X-Ca-Request-Mode": "debug",
                   "X-Ca-Key": "24955975",
                   "X-Ca-Stage": "RELEASE",
                   "Content-Type": "application/octet-stream; charset=utf-8",
                   "Authorization": "APPCODE 9c7eb663da9f4582a9d1a098c428d307"}
        r = requests.get(url=url, headers=headers)
        print(r.text)
        print(r.status_code)
        print(r.url)

a = web_requests()
a.Interface(input("area>"), input("need3HourForcast>"), input("needAlarm>"), input("needHourData>"),
            input("needIndex>"), input("needMoreDay>"))

输入参数,查看结果:


看了一下慕课的课程,改了一下较为标准的格式,并且获取13位时间戳

# coding: utf-8
import requests
import json
import random
import time

class web_requests(object):
    def __init__(self):
        pass

    def Interface(self, city):
        kv = {"area": city}
        url = "http://saweather.market.alicloudapi.com/spot-to-weather"
        headers = {"Host": "saweather.market.alicloudapi.com",
                   "X-Ca-Timestamp": "%s" % int(time.time()*1000),  # 获取13位时间戳
                                                                    # int向下取整
                   "gateway_channel": "http",
                   "X-Ca-Request-Mode": "debug",
                   "X-Ca-Key": "24955975",
                   "X-Ca-Stage": "RELEASE",
                   "Content-Type": "application/json;charset=utf-8",
                   "Authorization": "APPCODE 9c7eb663da9f4582a9d1a098c428d307"}

        r = requests.get(url=url, headers=headers, params=kv)
        print(r.text)
        print(r.status_code)
        print(r.url)

a = web_requests()
a.Interface(input(">"))
原文地址:https://www.cnblogs.com/p36606jp/p/15113589.html