資源簡介
# 可以這么用
weather = Weather_2345('廣州', '201811', '201901')
print(weather.get_weather())
# 也可以這么用
weather = Weather_2345('下面會再次設置區域這里可以隨便填', '201811', '201901')
area_list = ["白云", "從化", "花都", "海珠", "黃埔", "荔灣", "南沙", "番禺", "天河", "越秀", "增城"]
with open('./weather.log', 'w', encoding='utf-8') as fw:
fw.write("區域,日期,最高氣溫,最低氣溫,天氣,風向,風力,空氣指數,空氣情況,空氣等級" + "\n")
for area in area_list:
print()
weather.area = area
weather_list = weather.get_weather()
for lines in weather_list:
fw.write(",".join(lines) + '\n')
代碼片段和文件信息
#!/usr/bin/env?python
#?-*-?coding:?utf-8?-*-
“““
__title__?=?‘‘
__author__?=?‘tiany‘
__mtime__?=?‘2019/1/25‘
#?code?is?far?away?from?bugs?with?the?god?animal?protecting?~
“““
import?demjson
import?requests
class?Weather_2345():
def?__init__(self?area?begin_time?end_time):
self.area?self.begin_time?self.end_time?=?area?begin_time?end_time
self.headers?=?{
“User-Agent“:?“Mozilla/5.0?(Macintosh;?Intel?Mac?OS?X?10_13_4)?AppleWebKit/537.36?(KHTML?like?Gecko)?Chrome/65.0.3325.181?Safari/537.36“?}
self.prefix_url?=?“http://tianqi.2345.com/t/wea_history/js/{1}/{0}_{1}.js“
self.code_url?=?“http://tianqi.2345.com/js/citySelectData.js“
self.code_str?=?None
def?get_weather(self):
‘‘‘
:return:?區域日期最高氣溫最低氣溫天氣風向風力空氣指數空氣情況空氣等級
‘‘‘
result?=?[]
time_list?=?self.__generate_time_list(self.begin_time?self.end_time)
for?curr_month?in?time_list:
area_code?=?self.get_areacode(self.area)
url?=?self.prefix_url.format(area_code?curr_month)
try:
response?=?requests.get(url?headers=self.headers).text[16:-1]
response_dict?=?demjson.decode(response)
city?=?response_dict[‘city‘]
for?line?in?response_dict[‘tqInfo‘]:
if?line:
if?“aqi“?in?line:
result.append((city?line[“ymd“]?line[“bWendu“].rstrip(“℃“)?line[“yWendu“].rstrip(“℃“)
???line[“tianqi“]?line[“fengxiang“]?line[“fengli“]?line[“aqi“]
???line[“aqiInfo“]?line[“a
評論
共有 條評論