requests的post请求

requests的post请求

说明:这里使用聚合数据中的根据GPS查询全国天气数据接口。

接口地址:http://v.juhe.cn/weather/geo
返回格式:json/xml
请求方式:http get/post
请求示例:http://v.juhe.cn/weather/geo?format=2&key=您申请的KEY&lon=116.39277&lat=39.933748
AppKey:265516ed24e2dc54b09becbcba2a546e

JSON返回示例: 

{
    "resultcode": "200",
    "reason": "查询成功!",
    "result": {
        "sk": {	/*当前实况天气*/
            "temp": "21",	/*当前温度*/
            "wind_direction": "西风",	/*当前风向*/
            "wind_strength": "2级",	/*当前风力*/	
            "humidity": "4%",	/*当前湿度*/
            "time": "14:25"	/*更新时间*/
        },
        "today": {
            "city": "天津",
            "date_y": "2014年03月21日",
            "week": "星期五",
            "temperature": "8℃~20℃",	/*今日温度*/
            "weather": "晴转霾",	/*今日天气*/
            "weather_id": {	/*天气唯一标识*/
                "fa": "00",	/*天气标识00:晴*/
                "fb": "53"	/*天气标识53:霾 如果fa不等于fb,说明是组合天气*/
            },
            "wind": "西南风微风",
            "dressing_index": "较冷", /*穿衣指数*/
            "dressing_advice": "建议着大衣、呢外套加毛衣、卫衣等服装。",	/*穿衣建议*/
            "uv_index": "中等",	/*紫外线强度*/
            "comfort_index": "",
            "wash_index": "较适宜",	/*洗车指数*/
            "travel_index": "适宜",	/*旅游指数*/
            "exercise_index": "较适宜",	/*晨练指数*/
            "drying_index": ""
        },
        "future": {	/*未来几天天气*/
            "day_20140321": {
                "temperature": "8℃~20℃",
                "weather": "晴转霾",
                "weather_id": {
                    "fa": "00",
                    "fb": "53"
                },
                "wind": "西南风微风",
                "week": "星期五",
                "date": "20140321"
            },
            "day_20140322": {
                "temperature": "9℃~21℃",
                "weather": "霾转多云",
                "weather_id": {
                    "fa": "53",
                    "fb": "01"
                },
                "wind": "东北风微风转东南风微风",
                "week": "星期六",
                "date": "20140322"
            },
            "day_20140323": {
                "temperature": "9℃~19℃",
                "weather": "阴",
                "weather_id": {
                    "fa": "02",
                    "fb": "02"
                },
                "wind": "南风微风",
                "week": "星期日",
                "date": "20140323"
            },
            "day_20140324": {
                "temperature": "8℃~19℃",
                "weather": "晴转多云",
                "weather_id": {
                    "fa": "00",
                    "fb": "01"
                },
                "wind": "西南风微风转南风微风",
                "week": "星期一",
                "date": "20140324"
            },
            "day_20140325": {
                "temperature": "9℃~20℃",
                "weather": "多云",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "南风微风",
                "week": "星期二",
                "date": "20140325"
            },
            "day_20140326": {
                "temperature": "10℃~19℃",
                "weather": "多云",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "南风微风",
                "week": "星期三",
                "date": "20140326"
            },
            "day_20140327": {
                "temperature": "11℃~20℃",
                "weather": "阴转多云",
                "weather_id": {
                    "fa": "02",
                    "fb": "01"
                },
                "wind": "南风微风转无持续风向微风",
                "week": "星期四",
                "date": "20140327"
            }
        }
    },
    "error_code": 0
}

 测试用例类似于前一篇文章get请求方式。

post请求代码:

import requests

"""
测试用例中数据请求成功的情况
"""
# #接口地址以及post请求的属性参数(这里使用聚合数据GPS坐标查询天气接口,post请求需要lon、lat和key参数值
url="http://v.juhe.cn/weather/geo"
para={"key":"265516ed24e2dc54b09becbcba2a546e","lon":"116.39277","lat":"39.933748"}
#使用requests发送请求
result=requests.post(url,para)
# #查看返回的状态码
print(result.status_code)
#查看返回的json数据以及json中指定名称的数值
result_json=result.json()
# print(result_json)
print(result_json["result"])

"""
测试用例中数据请求不成功的情况(key值不正确等情况)
"""
# url="http://v.juhe.cn/weather/geo"
# para={"key":"265516ed24ee","lon":"116.39277","lat":"39.933748"}
# #使用requests发送请求
# result=requests.post(url,para)
# # #查看返回的状态码
# print(result.status_code)
# #查看返回的json数据以及json中指定名称的数值
# result_json=result.json()
# # print(result_json)
# print(result_json["result"])

实现结果:

发布了172 篇原创文章 · 获赞 76 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/dpl12/article/details/103230306
今日推荐