资源简介
Python 天气 爬虫 一个简单Python 代码 爬取天气信息 搬运工

代码片段和文件信息
import urllib.request
import gzip
import json
print(‘------天气查询------‘)
def get_weather_data() :
city_name = input(‘请输入要查询的城市名称:‘)
url1 = ‘http://wthrcdn.etouch.cn/weather_mini?city=‘+urllib.parse.quote(city_name)
url2 = ‘http://wthrcdn.etouch.cn/weather_mini?citykey=101010100‘
#网址1只需要输入城市名,网址2需要输入城市代码
#print(url1)
weather_data = urllib.request.urlopen(url1).read()
#读取网页数据
weather_data = gzip.decompress(weather_data).decode(‘utf-8‘)
#解压网页数据
weather_dict = json.loads(weather_data)
#将json数据转换为dict数据
return weather_dict
def show_weather(weather_data):
weather_dict = weather_data
#将json数据转换为dict数据
if weather_dict.get(‘desc‘) == ‘invilad-citykey‘:
print(‘你输入的城市名有误,或者天气中心未收录你所在城市‘)
elif weather_dict.get(‘desc‘) ==‘OK‘:
forecast = weather_dict.get(‘data‘).get(‘forecast‘)
print(‘城市:‘weather_dict.get(‘data‘).get(‘city‘))
print(‘温度:‘weather_dict.get(‘data‘).get(‘wendu‘)+‘℃ ‘)
print(‘感冒:‘weather_dict.get(‘data‘).get(‘ganmao‘))
print(‘风向:‘forecast[0].get(‘fengxiang‘))
print(‘风级:‘forecast[0].get(‘fengli‘))
print(‘高温:‘forecast[0].get(‘high‘))
print(‘低温:‘forecast[0].get(‘low‘))
print(‘天气:‘forecast[0].get(‘type‘))
print(‘日期:‘forecast[0].get(‘date‘))
print(‘*******************************‘)
four_day_forecast =input(‘是否要显示未来四天天气,是/否:‘)
if four_day_forecast == ‘是‘ or ‘Y‘ or ‘y‘:
for i in range(15):
print(‘日期:‘forecast[i].get(‘date‘))
print(‘风向:‘forecast[i].get(‘fengxiang‘))
print(‘风级:‘forecast[i].get(‘fengli‘))
print(‘高温:‘forecast[i].get(‘high‘))
print(‘低温:‘forecast[i].get(‘low‘))
print(‘天气:‘forecast[i].get(‘type‘))
print(‘--------------------------‘)
print(‘***********************************‘)
show_weather(get_weather_data())
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2262 2015-06-07 14:51 weather2.py
- 上一篇:HMM预测天气,python实现
- 下一篇:朴素贝叶斯算法python底层代码
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论