python request请求数据

发布时间 2023-09-19 08:56:05作者: 西北逍遥

python request请求数据

# -*- coding:utf-8 -*-
import requests
import json

#查询塔吊X数据
def searchTowerXValue():
    towerXValue = 0.0
    try:
        # 从服务器请求数据
        response = requests.get('https://www.baidu.com:8087/sX')

        # 检查响应状态码
        if response.status_code == 200:
            # 响应成功,处理数据
            data = response.text
            data2 = json.loads(data)
            print(data2)
            print(type(data2))
            towerXValue = data2['numberValue']
            print(towerXValue)
        else:
            # 响应失败,抛出异常或进行处理
            raise ValueError('服务器返回错误状态码:{}'.format(response.status_code))

    except requests.exceptions.RequestException as e:
        # 处理请求异常
        print('请求发生异常:{}'.format(e))

    except ValueError as e:
        # 处理值错误异常
        print('值错误异常:{}'.format(e))

    except Exception as e:
        # 处理其他异常
        print('发生其他异常:{}'.format(e))
    #
    return towerXValue
#
searchTowerXValue()

    

 

 

 

 

#############################