城市天气查询(Python)
时间:2006-09-04 来源:bbflyerwww
# coding: cp936
"""
查询城市天气情况
信息来源网 weather.news.sohu.com
Author : 寒江风
QQ : 84640008
Date : 2006-09-05
"""
import urllib
import re
def main():
# Show Information
print '中国城市天气查询'
# Get Parameters
city = raw_input('请输入您要查询天气的城市名字(如汕头,广州...) : ')
# Request For The Web Page
params = urllib.urlencode({'city':city})
webpage = urllib.urlopen('http://weather.news.sohu.com/city_inc.php?%s' % params)
webdata = webpage.read().replace('\n', '')
# Match The Useful Information
pattern = re.compile('<td class=blu01>([^<]*)<br>([^<]*)<br>([^<]*)<br>([^<]*)')
x = 0
for weather in pattern.findall(webdata):
if x == 0:
print '今日天气预报:'
elif x == 1:
print '明日天气预报:'
else:
print '后日天气预报:'
for i in range(4):
print weather[i].strip()
x += 1
if __name__ == '__main__':
main()
思想:通过网页链接提交进行天气查询,获取查询结果,然后进行正则匹配,提取有用信息