以前的几个天气预报的接口没法用了,新找了两个接口
一个接口启用了gzip,用代码处理的时候,需要注意下
json-handle的插件地址,可以很方便的解析json http://jsonhandle.sinaapp.com/
代码1
#encoding=utf8
import urllib
import urllib.request
import http.cookiejar
import json
import gzip
User_Agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0'
header = {}
header['User-Agent'] = User_Agent
weatherUrl='http://wthrcdn.etouch.cn/weather_mini?citykey=101110101'
urlFile = urllib.request.urlopen(weatherUrl)
jsonstr = gzip.decompress(urlFile.read()).decode("utf-8")
urlFile.close()
print("load page success")
#print(jsonstr)
jsondata=json.loads(jsonstr)
weather=jsondata['data']
'''
print("城市:"+weather['city'])
print("温度:"+weather['wendu'])
print("明天天气:"+weather['forecast'][0]['type'])
print("明天最高温度:"+weather['forecast'][0]['high'])
print("明天最低温度:"+weather['forecast'][0]['low'])
print("明天最低温度:"+weather['forecast'][0]['low'])
'''
printStr="当前温度"+weather['wendu']+",明天预报,"+weather['forecast'][0]['high']+weather['forecast'][0]['low']+","+weather['forecast'][0]['fengli']+weather['forecast'][0]['fengxiang']
print(printStr)
代码2
#encoding=utf8
import urllib
import urllib.request
import http.cookiejar
import json
User_Agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0'
header = {}
header['User-Agent'] = User_Agent
weatherUrl='http://www.weather.com.cn/data/sk/101110102.html'
urlFile = urllib.request.urlopen(weatherUrl)
jsonstr = urlFile.read()
urlFile.close()
jsonstr = jsonstr.decode('utf-8',errors='ignore')
print("load page success")
print(jsonstr)
jsondata=json.loads(jsonstr)
weather=jsondata['weatherinfo']
print("城市:"+weather['city'])
print("当前温度:"+weather['temp'])
print("风:"+weather['WD']+weather['WS'])
print("湿度:"+weather['SD'])
print("更新时间"+weather['time'])
printStr=weather['city']+"的当前温度是"+weather['temp']+","+weather['WD']+weather['WS']+",湿度"+weather['SD']+"。最后更新时间"+weather['time']
print(printStr)
分享到:
相关推荐
js版得,可能显示的比较慢,显示不出来刷新下,经本人测试,可以显示出来,先去QQ查看ip,然后把ip转换为城市,最后由城市代码去查看天气
全国天气预报接口根据城市名/id查询天气,向开发者提供的准确、稳定、丰富的天气数据云服务。
自动获取当天天气和未来五天天气,很不错 素材什么都有。
本工具使用C#语言开发WinFrom软件,根据国家气象局天气预报接口获取未来十五天天气预报。只用来新手学习,里边主要涉及到Url请求,Json转List,Json转model。
完整的天气预报接口,免费的API接口,下载可运行,Java实现,放入项目可直接运行 Springboot,Schedule ,Lombok,hutool,完整爬取过程,从0到一完整实现
2023最新ajax请求第三方接口天气预报案例.docx2023最新ajax请求第三方接口天气预报案例.docx2023最新ajax请求第三方接口天气预报案例.docx2023最新ajax请求第三方接口天气预报案例.docx2023最新ajax请求第三方接口...
利用Ajax的XMLHTTP异步交互技术读取天气预报服务器上的数据,来动态的显示各个地方的天气预报信息
自动联网获取天气预报是一个自动联网获取天气信息并且可以在指定时间发送给某个人的小例子源码,其实这个例子非常适合一些情侣或者家人朋友之间使用,如果再稍微扩展一下就是一个不错的例子,例如每天给异地的女友...
接口交互数据,通过sax解析xml。自己写的天气预报结果数据的解析。
jquery ajax方式获取天气预报接口数据, 解压就可以用, 包含图片资源文件, 样式比较漂亮, 下载看看吧
1、封装WeatherInfo实体类,对接口返回的结果进行映射的实体类 2、WeatherUtils类,负责调用第三方天气预报api接口处理的工具类,包括根据城市查询等,返回的格式是json 3、test类,对接口进行功能性测试
描述信息: http://blog.csdn.net/genfeng/article/details/31098021 RESTful学习笔记
Spring Boot项目:使用第三方天气API接口实现天气预报功能;使用redis缓存数据库进行数据缓存 接口示例:http://t.weather.sojson.com/api/weather/city/101010100
自动获取当天天气和未来五天天气,很不错 素材什么都有。
json获取天气预报源码,提供gb2312和utf-8两种方式的获取,欢迎各位朋友下载使用
本项目是一个自动联网获取天气信息并且可以在指定时间发送给某个人的小例子源码,其实这个例子非常适合一些情侣或者家人朋友之间使用,如果再稍微扩展一下就是一个不错的例子,例如每天给异地的女票定时发天气信息...
利用天气预报的api,在前台显示天气预报,有时候在开发某系统时,可能需要查看未来几天的天气情况,不用利用爬虫技术,也可以轻松的获取到未来几天的天气情况,
java获取实时天气状况。介绍了两种方法:1.通过httpclient请求中央气象台API接口获取xml数据 2.分析中央气象台页面源码,通过htmlunit技术从html中抓取实时天气信息
自己编写的天气预报小程序,使用了sqlit数据库,xml文件解析等方法。
通过网络上的服务 获取网络上的资源 主要就是如何通过提供的接口进行连接