python_weather/test.py

41 lines
1019 B
Python

import json
from datetime import datetime
import requests
headers = {
'Accept': '*/*',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Referer': 'http://www.weather.com.cn/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
}
timestamp = str(int(datetime.timestamp(datetime.now())))
params = {
'_': timestamp,
}
response = requests.get(
'http://d1.weather.com.cn/calendar_new/2024/101040200_202405.html',
params=params,
headers=headers,
verify=False,
)
content = response.text
encoding = response.encoding
decoded_html_content = content.encode(encoding).decode('utf-8')
json_str = decoded_html_content[len("var fc40 = "):]
# 解析 JSON 字符串为字典
data_dict = json.loads(json_str)
ls = []
for item in data_dict:
d = dict()
d["date"] = item["date"]
d["hgl"] = item["hgl"]
d["hmax"] = item["hmax"]
d["hmin"] = item["hmin"]
ls.append(d)
print(ls)