想爬出某个时点所有location的数据,从而得到这些点位的命名,尝试了一下报错了,怎么解?
import requests
time_point = "2022-05-01T12:00:00"
url = "https://uapi.ust.hk/energy/consumption/bms/_search?q=(@timestamp:["+ time_point + "])&size=1000"
payload = {}
headers = {
'UAuthorization': '5d0c4f23de394e0001045b5d74e4b1f00ceb4b9288ec5878ac708ebb'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
{"error":{"root_cause":[{"type":"parse_exception","reason":"parse_exception: Encountered \" \"]\" \"] \"\" at line 1, column 32.\nWas expecting:\n \"TO\" ...\n "}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"reindexed-v7-energy_consumption_bms","node":"0HIByJc1R8KODgtOBcy-ng","reason":{"type":"query_shard_exception","reason":"Failed to parse query [(@timestamp:[2022-05-01T12:00:00])]","index_uuid":"QK7csNsNSVCD6B8M6NeKHA","index":"reindexed-v7-energy_consumption_bms","caused_by":{"type":"parse_exception","reason":"parse_exception: Cannot parse '(@timestamp:[2022-05-01T12:00:00])': Encountered \" \"]\" \"] \"\" at line 1, column 32.\nWas expecting:\n \"TO\" ...\n ","caused_by":{"type":"parse_exception","reason":"parse_exception: Encountered \" \"]\" \"] \"\" at line 1, column 32.\nWas expecting:\n \"TO\" ...\n "}}}}],"caused_by":{"type":"parse_exception","reason":"parse_exception: Encountered \" \"]\" \"] \"\" at line 1, column 32.\nWas expecting:\n \"TO\" ...\n "}},"status":400}
不要用中括号,用双引号就OK了
import requests
time_point = '(@timestamp:"2022-05-01T12:00:00")'
url = "https://uapi.ust.hk/energy/consumption/bms/_search"
payload = {}
headers = {
'UAuthorization': '5d0c4f23de394e0001045b5d74e4b1f00ceb4b9288ec5878ac708ebb'
}
params = {'q': time_point,'size': 1000,}
response = requests.get(url, headers=headers,params =params, data =payload)
js = response.json()
print(js)
{'took': 7, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 281, 'relation': 'eq'}, 'max_score': 1.0, 'hits': [{'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'uWB6J4cB5uCQZMgwazPA', '_score': 1.0, '_source': {'@timestamp': '2022-05-01T12:00:00.000Z', 'consumption': '3969060', 'location': '9600_400A_TPN_MCCB_(TX-C06)_kWh_TL20_1hrs_Archive', 'dv_device_type': 'energy_consumption_bms_hourly', '@version': '1'}}, {'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'yWB6J4cB5uCQZMgwcbmo', '_score': 1.0, '_source': {'@timestamp': '2022-05-01T12:00:00.000Z', 'consumption': '3902440', 'location': '9600_1250A_TPN_ACB_(TX3-B02)_kWh_TL21_1hrs_Archive', 'dv_device_type': 'energy_consumption_bms_hourly', '@version': '1'}}, {'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'bmJ6J4cB5uCQZMgwfQXY', '_score': 1.0, '_source': {'@timestamp': '2022-05-01T12:00:00.000
Z', 'consumption': '37684', 'location': '5600_MPA_400A_N_11.1_kWh_TL_Archive', 'dv_device_type': 'energy_consumption_bms_hourly', '@version': '1'}}, {'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'bV96J4cB5uCQZMgwZq2u', '_score': 1.0, '_source': {'@timestamp': '2022-05-01T12:00:00.000Z', 'consumption': '2367120', 'location': '9600_630A_TPN_MCCB_(TX3-C03)_kWh_TL19_1hrs_Archive', 'dv_device_type': 'energy_consumption_bms_hourly', '@version': '1'}}, {'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'WGF6J4cB5uCQZMgwdj-f', '_score': 1.0, '_source': {'@timestamp': '2022-05-01T12:00:00.000Z', 'consumption': '5951630', 'location': '9600_800A_TPN_ACB_(TX3-C04)_kWh_TL22_1hrs_Archive', 'dv_device_type': 'energy_consumption_bms_hourly', '@version': '1'}}, {'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'Z2F6J4cB5uCQZMgwe8VT', '_score': 1.0, '_source': {'@timestamp': '2022-05-01T12:00:00.000Z', 'consumption': '1577720', '
location': '9600_400A_TPN_MCCB_(TX3-C05)_kWh_TL23_1hrs_Archive', 'dv_device_type': 'energy_consumption_bms_hourly', '@version': '1'}}, {'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'IkF5J4cB5uCQZMgwGRs7', '_score': 1.0, '_source': {'@timestamp': '2022-05-01T12:00:00.000Z', 'consumption': '92230', 'location': '9600_63A_TPN_MCCB_(TX1-C04)_kWh_TL01_1hrs_Archive', 'dv_device_type': 'energy_consumption_bms_hourly', '@version': '1'}}, {'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'Y0N5J4cB5uCQZMgwOzUy', '_score': 1.0, '_source': {'@timestamp': '2022-05-01T12:00:00.000Z', 'consumption': '4035850', 'location': '9600_160A_TPN_MCCB_(FSI-C07)_kWh_TL06_1hrs_Archive', 'dv_device_type': 'energy_consumption_bms_hourly', '@version': '1'}}, {'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'wFx6J4cB5uCQZMgwPwBl', '_score': 1.0, '_source': {'@timestamp': '2022-05-01T12:00:00.000Z', 'consumption': '44', 'location': '9600_800A
_4P_ACB_ATS_kWh_TL10_1hrs_Archive',{'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'LvT85IMBDGhMZh5WFplH', '_score': 1.0, '_source': {'consumption': '27314300', 'dv_device_type': 'energy_consumption_bms_hourly', 'location': 'NAB', '@version': '1', '@timestamp': '2022-05-01T12:00:00.000Z'}}, {'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'gIL1B4QBn69mLTKtaVBg', '_score': 1.0, '_source': {'@timestamp': '2022-05-01T12:00:00.000Z', 'consumption': '6544870', 'dv_device_type': 'energy_consumption_bms_hourly', 'location': 'A1-A2', '@version': '1'}}, {'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'UIP1B4QBn69mLTKtd3_F', '_score': 1.0, '_source': {'@timestamp': '2022-05-01T12:00:00.000Z', 'consumption': '16427900', 'dv_device_type': 'energy_consumption_bms_hourly', 'location': 'Enterprise-Center', '@version': '1'}}, {'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'pYT1B4QBn69mLTKtgnt8', '_score': 1.0, '_source': {'@timestamp': '2022-05-01T12:00:00.000Z', 'consumption': '3408180', 'dv_device_type': 'energy_consumption_bms_hourly', 'location': 'L1-Zone', '@version': '1'}}, {'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'y4T1B4QBn69mLTKth_Kk', '_score': 1.0, '_source': {'@timestamp': '2022-05-01T12:00:00.000Z', 'consumption': '27314300', 'dv_device_type': 'energy_con
sumption_bms_hourly', 'location': 'NAB', '@version': '1'}}, {'_index': 'reindexed-v7-energy_consumption_bms', '_type': '_doc', '_id': 'doX1B4QBn69mLTKtj57a', '_score': 1.0, '_source': {'@timestamp': '2022-05-01T12:00:00.000Z', 'consumption': '9943890', 'dv_device_type': 'energy_consumption_bms_hourly', 'location': 'UG7', '@version': '1'}}]}}
url = "https://uapi.ust.hk/energy/consumption/bms/_search?q=(@timestamp:["+ time_point + "])&size=1000"
其中的中括号导致了编码的问题,去掉就好了
import requests
url = "https://uapi.ust.hk/energy/consumption/bms/_search"
payload={}
headers = {
'UAuthorization': '5d0c4f23de394e0001045b5d74e4b1f00ceb4b9288ec5878ac708ebb'
}
response = requests.request("GET", url, headers=headers, data=payload).json()
print(response)
如果是需要根据条件查询,请提供API接口文档或者在线网页接口地址。
我看了下你这个报错的信息,显示400错误,这个错误一般是由于前后台不一致导致的,一般体现在你请求的参数或者参数的类型和后台要求的不一样,因此你可以再检查下你的参数是否正确,以及参数的类型是否正确。如果还不行,可能是接口有问题。
您可以先找到目标API并获取数据接口,然后使用 Python 的 requests 库发送 GET 请求获取数据。根据你的需求,对所返回的数据进行逐项解析,获取每个 location 的信息和命名。
如果该 API 提供了时间过滤参数,您可以设置起始时间点和终止时间点,以便仅获取一个时段内的数据。然后遍历这些数据,并判断每一行或每一项数据是否为 "location",将它们保存在一个列表中,再从中提取出命名。
需要特别提醒的是,在爬取数据时请确保您有权获得这些信息,并且符合相关的法律要求,不要向已阻止被爬虫访问的网站发送请求。同时请注意良好的数据道德实践,不抄袭、不盗用他人数据,以及尊重他人隐私。
根据报错信息看,问题可能出在查询语句 (@timestamp:["+ time_point + "]) 中。
看起来是缺少了 TO 操作符,导致无法正确解析查询语句。试着修改一下查询语句,加上 TO 操作符,如下所示:
q=(@timestamp:[" + time_point + " TO *])
这样,查询语句应该就可以正确解析了。
完整代码如下:
import requests
time_point = "2022-05-01T12:00:00"
url = "https://uapi.ust.hk/energy/consumption/bms/_search?q=(@timestamp:[" + time_point + " TO *])&size=1000"
payload = {}
headers = {
'UAuthorization': '5d0c4f23de394e0001045b5d74e4b1f00ceb4b9288ec5878ac708ebb'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
请注意确保您在请求头中提供了正确的授权令牌。