源码如下:
- #!/usr/bin/python
-
-
'''
-
此例对一个XML文件(str类型)进行分割取值,'list'类型不能进行split操作;取值超过1个时,即变成list类型。
-
weather.split('</item>')[1]先用“”分割,取得第二个元素。split('<description>')[1]
-
再用'<description>'分割这个元素,取得分割后的第二个元素,依次类推
-
strip():去掉空格
-
strip('\n'):去掉换行符
-
replace(' ', ''):替换掉多余空格
-
'''
-
-
import urllib2
-
weather = urllib2.urlopen('').read()
-
-
next_weather1 = weather.split('')[1]
-
next_weather2 = weather.split('')[1].split('
' )[1] -
next_weather2_1 = weather.split('')[1].split('
' )[0:1] -
next_weather3 = weather.split('')[1].split('
' )[1].strip('\n') -
next_weather4 = weather.split('')[1].split('
' )[1].strip('\n').split('\n')[1] -
next_weather5 = weather.split('')[1].split('
' )[1].strip('\n').split('\n')[1].split(',')[0] -
next_weather6 = weather.split('')[1].split('
' )[1].strip('\n').split('\n')[1].split(',')[0].replace(' ', '') -
next_weather7 = weather.split('')[1].split('
' )[1].strip('\n').split('\n')[1].split(',')[0].strip() -
-
print type(weather)
-
print weather
-
print ('---------------------------')
-
print next_weather1
-
print ('---------------------------')
-
print next_weather2
-
print ('---------------------------')
-
#print next_weather2_1
-
#print type(next_weather2_1)
-
#print ('---------------------------')
-
print next_weather3
-
print ('---------------------------')
-
print next_weather4
-
print ('---------------------------')
-
print next_weather5
-
print ('---------------------------')
-
print next_weather6
-
print ('---------------------------')
- print next_weather7
运行结果
- >>> ================================ RESTART ================================
- >>>
The Weather Network - Charlottetown, Prince Edward Island - http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs
The Weather Network - Canada's number one source for weather information in both the cable and web spaces. Copyright The Weather Network. Use of this RSS feed is subject to the Terms of Use as defined at en-us http://www.theweathernetwork.com/common/images/rss/rss_logo_en.jpg The Weather Network - http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs
http://blogs.law.harvard.edu/tech/rss Current Weather http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs - http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs
Fri, 30 Dec 2011, 08:00:00 AST spacer - http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs
Clear, - -10 °C , Humidity 79% , Wind
- calm
Sunday, January 1, 2012 Fri, 30 December, 2011, 06:46:00 AST - Light rain,
- High 3 °C, Low 0 °C, P.O.P. 90%
Monday, January 2, 2012 Fri, 30 December, 2011, 06:46:00 AST - Cloudy with showers,
- High 7 °C, Low 0 °C, P.O.P. 70%
Tuesday, January 3, 2012 Fri, 30 December, 2011, 06:46:00 AST - Variable cloudiness,
- High -2 °C, Low -8 °C, P.O.P. 20%
- ---------------------------
Sunday, January 1, 2012 Fri, 30 December, 2011, 06:46:00 AST - Light rain,
- High 3 °C, Low 0 °C, P.O.P. 90%
- ---------------------------
- Light rain,
- High 3 °C, Low 0 °C, P.O.P. 90%
- ---------------------------
- Light rain,
- High 3 °C, Low 0 °C, P.O.P. 90%
- ---------------------------
- Light rain,
- ---------------------------
- Light rain
- ---------------------------
- Light rain
- ---------------------------
- Light rain
- >>>