基于Python获取城市近7天天气预报

yipeiwu_com6年前Python基础

这篇文章主要介绍了基于Python获取城市近7天天气预报,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

首先,我们打开中国天气网,找到黄石市近7天天气的网页。

http://www.weather.com.cn/weather/101200601.shtml

然后按F12开始分析网页结构,找到各个标签,并分析它们的作用。h1:日期;p:天气;tem-span:最高温;tem-i:最低温;win:风;em:风向;win-i:风力。

接下来,我们需要找到它的用户代理,即User-agent。

分析的差不多了,我们就开始写代码,下面是我写的全部代码及运行结果:

import re
import requests
from bs4 import BeautifulSoup

def get_page(url): #获取URL
  try:
    headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36'}
    r = requests.get(url,headers)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    return r.text
  except:
    return '出现异常' #异常处理,防止出现错误

def parse_page(html, weather_list):
  soup = BeautifulSoup(html, 'html.parser')
  day_list = soup.find('ul', 't clearfix').find_all('li')
  for day in day_list:
    date = day.find('h1').get_text()
    wea = day.find('p', 'wea').get_text()
    if day.find('p', 'tem').find('span'): #判断标签'p','tem'下是否有标签'span',以此判断是否有最高温
        tem_h = day.find('p', 'tem').find('span').get_text()
    else:
        tem_h = '' #最高温
    tem_l = day.find('p', 'tem').find('i').get_text() #最低温
    win1 = re.findall('(?<= title=").*?(?=")', str(day.find('p','win').find('em')))
    win2 = '-'.join(win1) #风向,win1-win2
    level = day.find('p', 'win').find('i').get_text() #风力
    weather_list.append([date, wea, tem_l, tem_h, win2, level])


def print_wea(weather_list):
  s = ' \t' * 3
  print(s.join(('日期', '天气', '最低温', '最高温', '风向', '风力')))
  for i in weather_list:
    print(i[0], '\t',i[1],'\t\t\t',i[2],'\t\t\t',i[3],'\t\t',i[4],'\t\t',i[5]) #按格式输出

def main():
  url = 'http://www.weather.com.cn/weather/101200601.shtml'
  html = get_page(url)
  wea_list = []
  parse_page(html, wea_list)
  print("\t\t\t\t\t\t\t\t\t黄石市近7天天气预报")
  print_wea(wea_list)

if __name__ == '__main__':
  main()

在格式输出这方面,我的这份代码还存在着很大的缺陷,把它发出来,欢迎大家跟我一起讨论,改进。

相关文章

python中实现字符串翻转的方法

具体代码如下所示: #字符串反转 def reverse (s): rt = '' for i in range(len(s)-1,-1,-1): rt += s[i...

python 按不同维度求和,最值,均值的实例

python 按不同维度求和,最值,均值的实例

当变量维数加大时很难想象是怎样按不同维度求和的,高清楚一个,其他的应该就很清楚了,什么都不说了,上例子,例子一看便明白….. a=range(27) a=np.array(a) a=...

Python秒算24点实现及原理详解

什么是24点 我们先来约定下老王和他媳妇玩的24点规则:给定4个任意数字(0-9),然后通过+,-,*,/,将这4个数字计算出24。 小时候玩的都是这个规则,长大了才有根号,才有各种莫...

django-crontab 定时执行任务方法的实现

需求 每天请求一封邮件,并读取该邮件 这个其实可以使用linux 自带了crontab实现,但是毕竟是django 开发。想着不知道有没有方法可以从django 中实现。 简单搜索了下...

对python中的try、except、finally 执行顺序详解

如下所示: def test1(): try: print('to do stuff') raise Exception('hehe') print('to r...