天气预报API
功能
从中国天气网抓取数据返回1-7天的天气数据,包括:日期、天气、温度、风力、风向.
| |
| |
| |
| def get_weather(city:str): |
检查网页源代码可知,我们需要的数据在tag=li,且class=’skyskyid lv2 on‘或者class=’skyskyid lv2‘中,注意看,这个lv2不同的城市可能不同,因此需要用到正则表达式去匹配,剩下的就看代码了
| <ul class="t clearfix"> |
| <li class="sky skyid lv2 on"> |
| <h1>14日(今天)</h1> |
| <big class="png40 n00"></big> |
| <p title="晴" class="wea">晴</p> |
| <p class="tem"> |
| <i>0℃</i> |
| </p> |
| <p class="win"> |
| <em> |
| <span title="北风" class="N"></span> |
| </em> |
| <i>3级</i> |
| </p> |
| <div class="slid"></div> |
| </li> |
| <li class="sky skyid lv2"> |
| <h1>15日(明天)</h1> |
| <big class="png40 n00"></big> |
| <p title="晴" class="wea">晴</p> |
| <p class="tem"> |
| <span>15℃</span>/ <i>0℃</i> |
| </p> |
| <p class="win"> |
| <span title="西南风" class="SW"></span> |
| <span title="西风" class="W"></span> |
| <i>3级</i> |
| </p> |
| <div class="slid"></div> |
| </li> |
| <li class="sky skyid lv2"> |
| <h1>16日(后天)</h1> |
| </li> |
| </ul> |
| import requests |
| import re |
| from bs4 import BeautifulSoup |
| |
| head = 'http://www.weather.com.cn/weather/' |
| suffix = '.shtml' |
| txt = 'city_code_in_weather_report.txt' |
| |
| |
| def __load_code(): |
| result = dict() |
| with open(txt, 'r', encoding='utf-8') as f: |
| line = f.readline() |
| while line != '': |
| line.strip() |
| if line is not None: |
| line = line.strip() |
| index = line.find('=') |
| code = line[:index] |
| city = line[index + 1:] |
| result[city] = code |
| line = f.readline() |
| return result |
| |
| |
| def get_list(s_html): |
| result = [] |
| for i in s_html: |
| arr = i.get_text().split('\n') |
| while True: |
| try: |
| arr.remove('') |
| except ValueError: |
| break |
| result.append(arr) |
| |
| return result |
| |
| |
| def wind_dir(s_html): |
| result = [] |
| pattern = r'title=".*"' |
| for i in s_html: |
| tags = i.find_all('span') |
| |
| for j in tags: |
| res = re.search(pattern, str(j)) |
| if res is not None: |
| result.append(res.group()[7:-2]) |
| return result |
| |
| |
| def get_html(soup, tag): |
| res = [] |
| html = soup.find_all(tag) |
| for i in html: |
| try: |
| |
| if i['class'][0] == 'sky' and i['class'][1] == 'skyid' and re.fullmatch('lv\d', str(i['class'][2])): |
| res.append(i) |
| except KeyError: |
| pass |
| return res |
| |
| |
| def get_weather(city:str): |
| dic = __load_code() |
| if city not in dic: |
| return None |
| r = requests.get(head + dic[city] + suffix) |
| r.encoding = r.apparent_encoding |
| html_doc = r.text |
| soup = BeautifulSoup(html_doc, 'html.parser') |
| ori_html = get_html(soup, 'li') |
| |
| w_list = get_list(ori_html) |
| wind_list = wind_dir(ori_html) |
| index = 0 |
| for i in w_list: |
| i.append(wind_list[index]) |
| index += 1 |
| |
| return w_list |
| |
| |
| if __name__ == "__main__": |
| print(get_weather('西安')) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架