python开发城市天气预报
两年在学习python中,稍微学习了一点爬虫技术,并且作者一直喜欢开发游戏,然后就有学习了pygame框架,作者奇思妙想让两者集合一下开发一个天气预报,那么这就行动。
先运行编译后的截图:
有一种星空颗粒的感觉对吧。
开始代码:
# -*- coding:utf-8 -*-
from urllib import request
from bs4 import BeautifulSoup
import os,sys,pygame
from pygame.locals import *
from random import randint
pygame.init()
导入我们所有的包
不会下载的,进入cmd控制台输入pip install 名
然后写一个主函数
def main:
pass
if __name__ == '__main__':
main()
开始写入内存
error = 0
try:
response = request.urlopen('http://www.weather.com.cn/weather/101250101.shtml')
html = response.read()
soup = BeautifulSoup(html,'html.parser',from_encoding='gbk')
#*********************************************************
#天气地名存入数据
plasename = soup.find_all('div',class_="crumbs fl")[0]
plase1=plasename.find_all('a')
plase2=plasename.find_all('span')
plase_info = []
p_ind = 0
for p1 in plase1:
plase_info.append(plase1[p_ind].get_text())
plase_info.append(plase2[p_ind].get_text())
p_ind += 1
plase_info.append(plase2[p_ind].get_text())
#天气地名存入数据
#本周的天气信息存入数据
itclass = soup.find_all(class_='t clearfix')[0]
itli=itclass.find_all('li')
info_data = []
for li in itli:
inf1 = li.find_all('h1')[0].get_text()
inf2 = li.find_all('p',class_='wea')[0].get_text()
inf3 = li.find_all('p',class_='tem')[0].get_text().strip('\n')
inf4 = li.find_all('p',class_='win')[0].get_text().strip('\n')
info_data.append([inf1,inf2,inf3,inf4])
#本周的天气信息存入数据
except:
error = 1
窗口和爬虫预备
#*********************************************************
#********************
#plase_info 地名信息
#info_data 天气信息
#使用pygame显示天气信息
#********************
screen = pygame.display.set_mode((602, 462))
caption = pygame.display.set_caption('天气预报')
#font = pygame.font.Font('./m.ttf',25)
#font2 = pygame.font.Font('./m.ttf',16)
font = pygame.sysfont.SysFont('microsoft yahei', 25)
font2 = pygame.sysfont.SysFont('microsoft yahei', 16)
fonte = pygame.sysfont.SysFont('microsoft yahei', 40)
fonte = fonte.render("网络没有连接",True, (100,200,255))
wp = 200
hp = 140
pos = []
color = (100,100,255)
flag = 0
ppxy = [0,0]
timer = 0
wh = [602, 462]
acpos = [[randint(150,903),randint(462, 562),randint(1, 2)]]
pygame.init()
事件循环
while True:
screen.fill([255,255,255])
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == MOUSEBUTTONDOWN:
mpos = event.pos
flag = 0
for p in pos:
if mpos[0]>p[0] and mpos[0]<p[0]+wp:
if mpos[1]>p[1] and mpos[1]<p[1]+hp:
flag = 1
ppxy = p
break
最后画出地名信息和天气信息然后一个动态颗粒动画。
#-------------------------画地名-----------------------
if error:
screen.blit(fonte,(wh[0]/2-fonte.get_width()/2,wh[1]/2-fonte.get_width()/2))
else:
index = 0
for pl in plase_info:
pf = font.render('%s'%pl, True, (100,200,100))
screen.blit(pf,(150+60*index,0))
index += 1
pygame.draw.line(screen,(255,0,128), (0,35), (640,35),5)
#-------------------------画地名-----------------------
#-------------------------画天气信息-----------------------
for i in range(3):
for j in range(3):
if j == 0:
w = 0
if j == 1:
w = 201
if j == 2:
w =402
if i == 0:
h = 40
if i == 1:
h = 181
if i == 2:
h = 322
pygame.draw.rect(screen,color,(w,h,wp,hp))
pos.append([w,h])
if flag:
pygame.draw.rect(screen,(255,0,128),(ppxy[0],ppxy[1],wp,hp))
index = 0
for info in info_data:
ind = 0
for io in info:
inf = font2.render('%s'%io, True, (255,255,128))
screen.blit(inf,(100-inf.get_width()/2+pos[index][0],20+pos[index][1]+23*ind))
ind += 1
index += 1
#-------------------------画天气信息-----------------------
timer += 1
if timer >= 2:
acpos.append([randint(150,903),randint(462, 562),randint(1, 2)])
timer = 0
index = 0
for p in acpos:
if acpos[index][2] == 1:
acpos[index][0] += -(p[2]+4)
acpos[index][1] += -(p[2]+4)
elif acpos[index][2] == 2:
acpos[index][0] += -(p[2]+2)
acpos[index][1] += -(p[2]+2)
index += 1
for p in acpos:
pygame.draw.rect(screen,(255,255,255),(p[0],p[1],p[2],p[2]))
index = 0
for p in acpos:
if p[0] < 0 or p[1]<0:
acpos.pop(index)
index += 1
pygame.time.delay(20)
pygame.display.update()
最终,作者通过pyinstaller打包发布了一下
项目链接奉上
https://download.csdn.net/download/qq_25755645/12235385?spm=1001.2014.3001.5503
本文含有隐藏内容,请 开通VIP 后查看