写一个python代码访问这个网站httpsycjhcomcnindexhtml获取源代码取得源代码中三个值分别是时间涨落和水位时间在stnm朱沱tm和wptn之间涨落在stnm朱沱tm之后的wptn和z之间如果取出的值为5则为涨水为4则为落水水位在stnm朱沱tm之后的z和之间取出值后就打印输出格式为朱沱水文站:更新时间:实时水位:16264m正在涨水不要用正则表达式
您可以使用Python的requests库来访问网站并获取源代码,然后使用字符串的查找和切片操作来提取所需的值。下面是一个示例代码:
import requests
# 发送GET请求获取网页源代码
response = requests.get("http://sy.cjh.com.cn/index.html")
source_code = response.text
# 查找时间
start_index = source_code.find('"stnm":"朱沱","tm":') + len('"stnm":"朱沱","tm":')
end_index = source_code.find('"wptn":', start_index)
time = source_code[start_index:end_index]
# 查找涨落
start_index = source_code.find('"stnm":"朱沱","tm":', end_index) + len('"stnm":"朱沱","tm":')
end_index = source_code.find('","z":', start_index)
wptn = source_code[start_index:end_index]
# 查找水位
start_index = source_code.find('","z":', end_index) + len('","z":')
end_index = source_code.find('"}', start_index)
water_level = source_code[start_index:end_index]
# 判断涨落值并生成输出字符串
if wptn == '5':
movement = "正在涨水"
elif wptn == '4':
movement = "正在落水"
else:
movement = ""
# 打印输出
print("朱沱水文站:更新时间:{},实时水位:{}m,{}".format(time, water_level, movement))
请注意,这只是一个示例代码,具体的查找位置和切片操作可能需要根据网页源代码的实际情况进行调整
原文地址: http://www.cveoy.top/t/topic/h8as 著作权归作者所有。请勿转载和采集!