- 帖子
- 12
- 精华
- 0
- 积分
- 42
- 阅读权限
- 10
- 注册时间
- 2017-8-17
- 最后登录
- 2017-11-1
|
crossin先生 发表于 2017-9-14 11:26
数据里面的确有一些类似笔误的错误,大多不是大城市
简单的处理办法就是加异常处理,忽略这些
楼主,我用新网址进行了替换,不过查到后面就突然跳出奇奇怪怪的东西了
# url = 'http://m.weather.com.cn/data3/city%s.xml'
# urlp = 'http://m.weather.com.cn/data3/city.xml'
url = 'http://www.weather.com.cn/data/list3/city%s.xml?level=3'
urlp = 'http://www.weather.com.cn/data/list3/city.xml?level=1'
300501|101300501
桂林:101300501
<!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" />
<script type="text/javascript">
var t=4;//设定跳转的时间
setInterval("refer()",1000); //启动1秒定时
function refer(){
if(t==0){
location="http://www.weather.com.cn/#404"; //#设定跳转的链接地址
}
document.getElementById('show').innerHTML=""+t+""; // 显示倒计时
t--; // 计数器递减
//本文转自:
}
</script>
<style>
【code】
# python 3.4.2
# L45 Get province code
# 20170908
# by oak
########################
import urllib.request
# url = 'http://m.weather.com.cn/data3/city%s.xml'
# urlp = 'http://m.weather.com.cn/data3/city.xml'
url = 'http://www.weather.com.cn/data/list3/city%s.xml?level=3'
urlp = 'http://www.weather.com.cn/data/list3/city.xml?level=1'
contentp = urllib.request.urlopen(urlp).read().decode('utf8') # 获取页面信息,python需要使用到decode
# print(contentp)
provinces = contentp.split(',')
print(provinces)
print()
for p in provinces:
p_info = p.split('|')
p_code = p_info[0]
p_name = p_info[1]
urlc = url % p_code # 拼接市级别的url
contentc = urllib.request.urlopen(urlc).read().decode('utf8') # 获取每个省对应城市的信息
print (contentc)
cities = contentc.split(',')
# print (cities)
print()
for c in cities:
c_info = c.split('|') # 将每个字符串变为数据,之后取第一个值,即为城市编码
c_code = c_info[0]
urld = url % c_code
contentd = urllib.request.urlopen(urld).read().decode('utf8')
print(contentd)
districts = contentd.split(',')
print()
for d in districts:
d_info = d.split('|')
d_code = d_info[0]
d_name = d_info[1]
urla = url % d_code
contenta = urllib.request.urlopen(urla).read().decode('utf8')
print(contenta)
code = contenta.split('|')[1]
print(d_name + ':' + code)
print()
|
|