设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

【Python 第44课】 查天气(2)

  [复制链接]

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

71#
发表于 2015-2-8 23:55:28 |只看该作者
未来的~布斯 发表于 2015-2-8 15:59
我的也是win7的,出现的问题和你一样,然后按照你的,把两个py文件的开头换成# -*- coding: utf-8 -*- 结 ...

试试改成 -*- coding: cp936 -*-
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

6

积分

新手上路

Rank: 1

72#
发表于 2015-2-13 17:15:14 |只看该作者
我的出现这样的东西
你想查哪个城市的天气?
北京
<!DOCTYPE HTML>
<html>
<head>
<link rel="dns-prefetch" href="http://i.tq121.com.cn">
<meta charset="utf-8" />
<title>非常抱歉,网页无法访问</title>
<meta  name="keywords" content="天气预报,天气,天气预报查询一周,天气预报15天查询,天气预报查询,北京天气,天气在线,气候,气象" />
<meta name="description" content="中国天气网官方权威发布天气预报,逐三小时天气预报,提供天气预报查询一周,天气预报15天查询,空气质量,生活指数,旅游出行,交通天气等查询服务" />
<meta name="msapplication-task" content="name=天气资讯;action-uri=http://news.weather.com.cn/index.shtml;icon-uri=http://www.weather.com.cn/favicon.ico" />
<meta name="msapplication-task" content="name=生活天气;action-uri=http://www.weather.com.cn/life/index.shtml;icon-uri=http://www.weather.com.cn/favicon.ico" />
<meta name="msapplication-task" content="name=气象科普;action-uri=http://www.weather.com.cn/science/index.shtml;icon-uri=http://www.weather.com.cn/favicon.ico" />
<meta name="msapplication-task" content="name=灾害预警;action-uri=http://www.weather.com.cn/alarm/index.shtml;icon-uri=http://www.weather.com.cn/favicon.ico" />
<meta name="msapplication-task" content="name=旅游天气;action-uri=http://www.weather.com.cn/trip/index.shtml;icon-uri=http://www.weather.com.cn/favicon.ico" />
<style>
body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, form, fieldset, p, blockquote, th, td, article, aside, summary, details, figcaption, figure, footer, header, hgroup, menu, nav, section { margin:0; padding:0 }
article, aside, summary, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display:block }
table { border-collapse:collapse; border-spacing:0 }
fieldset, img { border:0; vertical-align:middle }
address, caption, cite, code, dfn, em, strong, th, var { font-style:normal; font-weight:normal }
ul, ol { list-styleutside none none }
q:before, q:after { content:'' }
abbr, acronym { border:0 }
input, button, select { vertical-align:middle }
.fl { float:left; display:inline }
.fr { float:right; display:inline }
body { background-color:#fff; background-attachment:fixed; background-position:center top; background-repeat:no-repeat; padding:0; margin:0; border:0; font:14px/1.6 "Microsoft Yahei", "Tahoma", "SimSun" }
* { padding:0; margin:0; border:0 }
ul { list-style:none }
a { text-decoration:none; color:#252525 }
a:hover { color:#ee842f }
.clear { clear:both; height:0; overflow:hidden }
nav { display:block }
table { border-collapse:collapse }
h1, h, h3, h4, h5, h6 { font-weight:normal }
.clearfix { zoom:1 }
.clearfix:after { content:"."; display:block; clear:both; visibility:hidden; height:0 }
body { color:#666666; }
i { font-style:normal; }
.nide { width:1000px; margin:0 auto; }
.cuo { width:885px; margin:0 auto 60px; }
.cuo h1 { color:#636060; height:290px; line-height:290px; font-size:40px; font-weight:bold; }
.cuo h1 span { font-weight:bold; padding-right:20px; font-size:100px; }
.cuo p { margin-bottom:20px; font-size:23px; }
.cuo p a { color:#16459a; }
.cuo p a:hover{ color:#ee842f; }
.zuo ul li a img { width:155px; height:120px; }
.zuo { width:1021px; float:left; }
.zuo ul li { float:left; width:155px; text-align:center; height:120px; margin-right:15px; display:inline; }
.zuo ul li a { margin-bottom:10px; display:block; width:155px; }
.zuo ul { width:340px; float:left; }
#search .midBlock { position:relative; z-index:99; width:1000px; margin:0 auto; }
#search { height:64px; margin:16px auto; height:64px; border-bottom:3px solid #ccc; }
#search .midBlock .selCity { position:absolute; width:210px; height:120px; top:0px; left:390px; background-color:#ebebeb; padding:0px 20px 20px 20px; display:none; }
#search .midBlock .selCity p { text-align:left; }
#search .midBlock .selCity p.droplist { line-height:25px; z-index:99; position:relative; }
#search .midBlock .selCity p.title { color:#076EA8; }
#search h1 { display:block; float:left; width:140px; margin-top:15px; margin-right:10px; overflow:hidden; }
body { color:#666666; }
i { font-style:normal; }
.nide { width:1000px; margin:0 auto; }
.cuo { width:885px; margin:0 auto 60px; }
.cuo h1 { color:#636060; height:290px; line-height:290px; font-size:40px; font-weight:bold; }
.cuo h1 span { font-weight:bold; padding-right:20px; font-size:100px; }
.cuo p { margin-bottom:20px; font-size:23px; }
.cuo p a { color:#16459a; }
.zuo ul li a img { width:155px; height:120px; }
.zuo { width:1021px; float:left; }
.zuo ul li { float:left; width:155px; text-align:center; height:120px; margin-right:15px; display:inline; }
.zuo ul li a { margin-bottom:10px; display:block; width:155px; }
.zuo ul { width:340px; float:left; }
#search .midBlock { position:relative; z-index:99; width:1000px; margin:0 auto; }
#search { height:64px; margin:16px auto; height:64px; border-bottom:3px solid #ccc; }
#search .midBlock .selCity { position:absolute; width:210px; height:120px; top:0px; left:390px; background-color:#ebebeb; padding:0px 20px 20px 20px; display:none; }
#search .midBlock .selCity p { text-align:left; }
#search .midBlock .selCity p.droplist { line-height:25px; z-index:99; position:relative; }
#search .midBlock .selCity p.title { color:#076EA8; }
#search h1 { display:block; float:left; width:140px; height:35px; background:url(); margin-top:15px; margin-right:10px; overflow:hidden; }
#search h1 a { display:block; width:140px; height:35px; text-indent:-9999px; }
#txtZip { font-size: 14px; height: 35px; line-height: 35px; padding-left:10px; background:none; color: #6bb0da; margin-top: 2px; text-indent: 4px; width: 285px; }
#btnZip { background: none; cursor: pointer; height: 23px; text-indent: -9999px; width: 42px; }
</style>
</head>

<body>
<div id="search">
  <div class="midBlock">
    <h1> <a title="中国天气网" href="http://www.weather.com.cn">中国天气</a> </h1>
  </div>
</div>
<div class="nide">
  <div class="cuo">
    <h1><span><i style="color:#dd8d8d;">4</i><i style="color:#84b998">0</i><i style="color:#84adc2;">4</i></span>您访问的页面不存在</h1>
    <p>您可以:<a href="http://www.weather.com.cn">返回首页</a></p>
    <p>或者看看下面的美图吧!</p>
  </div>
  <div class="zuo">
<ul><li><a href="http://p.weather.com.cn/2015/02/zrds/2271339.shtml"  target="_blank" title="云飞鹤舞迎春到"><img alt="云飞鹤舞迎春到" src="http://pic.weather.com.cn/images/cn/photo/2015/02/13/EC380471C36008416FFCA00677877345_s.jpg" target="_blank"></a><a href="http://p.weather.com.cn/2015/02/zrds/2271339.shtml"  target="_blank" title="云飞鹤舞迎春到">云飞鹤舞迎春到</a></li><li><a href="http://p.weather.com.cn/2015/02/zrds/2271331.shtml"  target="_blank" title="烟台海滨现“千鸥争食”"><img alt="烟台海滨现“千鸥争食”" src="http://pic.weather.com.cn/images/cn/photo/2015/02/13/F4DF897BADAEAABBFF0F0C6CD2F04EB4_s.jpg" target="_blank"></a><a href="http://p.weather.com.cn/2015/02/zrds/2271331.shtml"  target="_blank" title="烟台海滨现“千鸥争食”">烟台海滨现“千鸥争食”</a></li></ul>




<ul><li><a href="http://p.weather.com.cn/2015/02/txqg/2271268.shtml"  target="_blank" title="摄影师高空拍赌城夜景 如电脑芯片"><img alt="摄影师高空拍赌城夜景 如电脑芯片" src="http://pic.weather.com.cn/images/cn/photo/2015/02/13/D135E474F543136F18157A985E9DA1C6_s.jpg" target="_blank"></a><a href="http://p.weather.com.cn/2015/02/txqg/2271268.shtml"  target="_blank" title="摄影师高空拍赌城夜景 如电脑芯片">摄影师高空拍赌城夜景 如电脑芯片</a></li><li><a href="http://p.weather.com.cn/2015/02/txqg/2269595.shtml"  target="_blank" title="加拿大冰雪酒店 顾客躺冰床入睡"><img alt="加拿大冰雪酒店 顾客躺冰床入睡" src="http://pic.weather.com.cn/images/cn/photo/2015/02/09/CA8138F07A556B6C3B2B59447ADECAF4_s.jpg" target="_blank"></a><a href="http://p.weather.com.cn/2015/02/txqg/2269595.shtml"  target="_blank" title="加拿大冰雪酒店 顾客躺冰床入睡">加拿大冰雪酒店 顾客躺冰床入睡</a></li></ul>




<ul><li><a href="http://p.weather.com.cn/2015/02/tqsh/2271565.shtml"  target="_blank" title="盘点全球最具个性的另类“玫瑰花”"><img alt="盘点全球最具个性的另类“玫瑰花”" src="http://pic.weather.com.cn/images/cn/photo/2015/02/13/C5EBFD231F9A718D8556C5A72744161A_s.jpg" target="_blank"></a><a href="http://p.weather.com.cn/2015/02/tqsh/2271565.shtml"  target="_blank" title="盘点全球最具个性的另类“玫瑰花”">盘点全球最具个性的另类“玫瑰花”</a></li><li><a href="http://p.weather.com.cn/2015/02/tqsh/2271487.shtml"  target="_blank" title="“我们俩”"><img alt="“我们俩”" src="http://pic.weather.com.cn/images/cn/photo/2015/02/13/5C33AFB987D73D1CBFC5997D16CB2FE9_s.jpg" target="_blank"></a><a href="http://p.weather.com.cn/2015/02/tqsh/2271487.shtml"  target="_blank" title="“我们俩”">“我们俩”</a></li></ul>




  </div>
</div>
<script type="text/javascript" src="http://c.wrating.com/a1.js">
</script>
<script type="text/javascript">
var vjAcc="860010-2099220100";
var wrUrl="http://c.wrating.com/";
vjTrack("");
</script>
<noscript>
<img src="http://c.wrating.com/a.gif?a=&c=860010-2099220100" width="1" height="1"/>
</noscript>

</body>
</html>
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

73#
发表于 2015-2-14 17:07:11 |只看该作者
nanshoua 发表于 2015-2-13 17:15
我的出现这样的东西
你想查哪个城市的天气?
北京

访问地址打错了或者失效了
得到的是个错误提示页面 “非常抱歉,网页无法访问”
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

10

积分

新手上路

Rank: 1

74#
发表于 2015-3-4 14:28:55 |只看该作者
请问大神们,我输入“北京”后,没有返回结果,这是什么问题呢,又如何解决??

代码如下:

# -*- coding: UTF-8 -*-
import urllib2
from city import city

exit=False

while not exit:
    cityname=raw_input("你想查询哪个城市的天气?输入q退出\n")
    if cityname=="q" or cityname=="Q":
        print("退出!")
        exit=True
    else:
        citycode=city.get(cityname)
        if citycode:
            url=("http://www.weather.com.cn/data/cityinfo/%s.html"%citycode)
            request=urllib.request.Request(url)
            response=urllib.request.urlopen(request)
            content=response.read().decode("utf-8")
            print(content)


搜狗截图20150304142055.png (8.47 KB, 下载次数: 251)

搜狗截图20150304142055.png

回复

使用道具 举报

0

主题

0

好友

10

积分

新手上路

Rank: 1

75#
发表于 2015-3-4 14:30:42 |只看该作者
crossin先生 发表于 2015-2-8 23:55
试试改成 -*- coding: cp936 -*-

我按你说的改过了,可是也不成功
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

76#
发表于 2015-3-5 13:52:33 |只看该作者
ccccccindy 发表于 2015-3-4 14:30
我按你说的改过了,可是也不成功

大概是现在接口变了,以前的接口已经失效了
http://m.weather.com.cn/atad/101230201.html
类似这个,不过好像也时好时坏
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

10

积分

新手上路

Rank: 1

77#
发表于 2015-11-10 11:00:53 |只看该作者
哪儿去下载城市附件city.py呀?
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

78#
发表于 2015-11-10 12:50:08 |只看该作者
naFgnaYuX 发表于 2015-11-10 11:00
哪儿去下载城市附件city.py呀?

查天气1里
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

10

积分

新手上路

Rank: 1

79#
发表于 2015-11-11 02:42:08 |只看该作者
crossin先生 发表于 2015-11-10 12:50
查天气1里

谢谢,之前是在百度找到你的word版看的,不明白,就找到论坛来了,回复这么快,谢谢了
回复

使用道具 举报

0

主题

1

好友

34

积分

新手上路

Rank: 1

80#
发表于 2015-11-11 23:53:47 |只看该作者
参考了楼上各种方法,最后是在idle打开city,然后修改utf-8为cp936,但是运行后,居然还是出现问题,问题反馈如下:
请教Crossin先生,出现以下错误,但是在浏览器可以打开,这种是被封了吗?是需要传说的伪装header才可以么?
  1. 你想查询哪个城市的天气?
  2. 北京
  3. 101010100

  4. http:\www.weather.com.cn/data/cityinfo/101010100.html  #添加测试url完整

  5. Traceback (most recent call last):
  6.   File "D:\2B Coder\lesson\crossin\practice\lesson44.py", line 23, in <module>
  7.     content=urllib2.urlopen(url).read()
  8.   File "C:\Python27\lib\urllib2.py", line 154, in urlopen
  9.     return opener.open(url, data, timeout)
  10.   File "C:\Python27\lib\urllib2.py", line 429, in open
  11.     req = meth(req)
  12.   File "C:\Python27\lib\urllib2.py", line 1125, in do_request_
  13.     raise URLError('no host given')
  14. URLError: <urlopen error no host given>
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-3 14:50 , Processed in 0.024887 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部