zoukankan      html  css  js  c++  java
  • 查天气43课-46课

    【43】查天气1 天气网的3个接口 不可用 百度 :天气网 接口 【44】查天气2 两个新模块 1,urllib2 2.json import urllib2 web=urllib2.urlopen('http://www.baidu.com') content=web.read(); f=file('baidu.html','w') f.write(content) f.close() 解决中文问题和城市代码问题用了大半天 中文问题的解决方法: 在代码和城市代码py文件中开头部分都加上 # -*- coding: cp936 -*- 城市代码部分 用excel和word完成城市代码文件的编写 代码 # -*- coding: cp936 -*- import urllib2 #import json from city import city #print city cityname=raw_input('你想查哪个城市的天气? ') citycode=city.get(cityname) if citycode: #not empty url='http://www.weather.com.cn/data/cityinfo/%s.html' %citycode content=urllib2.urlopen(url).read() print content else: print 'no' 其中city.py中文件的格式 # -*- coding: cp936 -*- city={ '北京':101010100, '海淀':101010200, '朝阳':101010300, ..... '玉山':101340903, '新港':101340904 } 【45】查天气3 代码 # -*- coding: cp936 -*- import urllib2 import json from city import city #print city cityname=raw_input('你想查哪个城市的天气? ') citycode=city.get(cityname) if citycode: #not empty url='http://www.weather.com.cn/data/cityinfo/%s.html' %citycode content=urllib2.urlopen(url).read() #字符串类型 data=json.loads(content)#字典类型 result=data['weatherinfo'] str_temp= ('%s %s~ %s')%( result ['weather'], result ['temp1'], result ['temp2']) print str_temp else: print '没有找到该城市' 加上try...catch后的代码 # -*- coding: cp936 -*- import urllib2 import json from city import city #print city cityname=raw_input('你想查哪个城市的天气? ') citycode=city.get(cityname) if citycode: #not empty try: url='http://www.weather.com.cn/data/cityinfo/%s.html' %citycode content=urllib2.urlopen(url).read() #字符串类型 data=json.loads(content)#字典类型 result=data['weatherinfo'] str_temp= ('%s %s~ %s')%( result ['weather'], result ['temp1'], result ['temp2']) print str_temp except: print'查询失败' else: print '没有找到该城市' 【46】查天气4 还没学习 这个部分还是很重要的
  • 相关阅读:
    Element-ui左侧菜单刷新依旧高亮显示当前菜单
    Element-ui表格单选
    Element-UI表格点击Popover 弹出框确定取消
    纯JS原生请求接口post方式
    Vue+Element-ui+二级联动封装组件
    Vue如何修改标题title呢?
    vue-cli3.0怎么修改端口?
    Element-ui上传文件(删除、添加、预览)
    关于Ubuntu的ifconfig命令出现SIOCSIFADDR系列错误
    关于keil 中出现“give arg types”
  • 原文地址:https://www.cnblogs.com/2012begin/p/3641395.html
Copyright © 2011-2022 走看看