PythonÖеÄsplit()Ó¦ÓÃ
(2011-12-31 08:37)
Ô´ÂëÈçÏ£º
- #!/usr/bin/python
-
-
'''
-
´ËÀý¶ÔÒ»¸öXMLÎļþ£¨strÀàÐÍ£©½øÐзָîȡֵ£¬'list'ÀàÐͲ»ÄܽøÐÐsplit²Ù×÷£»È¡Öµ³¬¹ý1¸öʱ£¬¼´±ä³ÉlistÀàÐÍ¡£
-
weather.split('</item>')[1]ÏÈÓá°</item>¡±·Ö¸î£¬È¡µÃµÚ¶þ¸öÔªËØ¡£split('<description>')[1]
-
ÔÙÓÃ'<description>'·Ö¸îÕâ¸öÔªËØ£¬È¡µÃ·Ö¸îºóµÄµÚ¶þ¸öÔªËØ£¬ÒÀ´ÎÀàÍÆ
-
strip()£ºÈ¥µô¿Õ¸ñ
-
strip('\n'):È¥µô»»Ðзû
-
replace(' ', ''):Ìæ»»µô¶àÓà¿Õ¸ñ
-
'''
-
-
import urllib2
-
weather = urllib2.urlopen('http://rss.theweathernetwork.com/weather/cape0005').read()
-
-
next_weather1 = weather.split('</item>')[1]
-
next_weather2 = weather.split('</item>')[1].split('<description>')[1]
-
next_weather2_1 = weather.split('</item>')[1].split('<description>')[0:1]
-
next_weather3 = weather.split('</item>')[1].split('<description>')[1].strip('\n')
-
next_weather4 = weather.split('</item>')[1].split('<description>')[1].strip('\n').split('\n')[1]
-
next_weather5 = weather.split('</item>')[1].split('<description>')[1].strip('\n').split('\n')[1].split(',')[0]
-
next_weather6 = weather.split('</item>')[1].split('<description>')[1].strip('\n').split('\n')[1].split(',')[0].replace(' ', '')
-
next_weather7 = weather.split('</item>')[1].split('<description>')[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 ================================
- >>>
- <type 'str'>
- <?xml version="1.0" encoding="ISO-8859-1"?><rss version="2.0">
- <channel>
- <title>The Weather Network - Charlottetown, Prince Edward Island</title>
- <link>http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs</link>
- <description>The Weather Network - Canada's number one source for weather information in both the cable and web spaces. </description>
- <copyright>Copyright The Weather Network. Use of this RSS feed is subject to the Terms of Use as defined at http://www.theweathernetwork.com/aboutus/copyright#rss</copyright>
- <language>en-us</language>
- <image>
- <url>http://www.theweathernetwork.com/common/images/rss/rss_logo_en.jpg</url>
- <title>The Weather Network</title>
- <link>http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs</link>
- </image>
- <docs>http://blogs.law.harvard.edu/tech/rss</docs>
- <item>
- <title>Current Weather</title>
- <guid>http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs</guid>
- <link>http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs</link>
- <pubDate>Fri, 30 Dec 2011, 08:00:00 AST</pubDate>
- <image>
- <url>http://rss.counter.weather.ca/counter.gif</url>
- <title>spacer</title>
- <link>http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs</link>
- </image>
- <description>Clear,
- -10&nbsp;&deg;C , Humidity 79% , Wind
- calm </description>
- </item>
- <item>
- <title>Sunday, January 1, 2012</title>
- <guid>http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=day1#ltermfx</guid>
- <link>http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=day1#ltermfx</link>
- <pubDate>Fri, 30 December, 2011, 06:46:00 AST</pubDate>
- <description>
- Light rain,
- High 3&nbsp;&deg;C, Low 0&nbsp;&deg;C, P.O.P. 90%
- </description>
- </item>
- <item>
- <title>Monday, January 2, 2012</title>
- <link>http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=day2#ltermfx</link>
- <guid>http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=day2#ltermfx</guid>
- <pubDate>Fri, 30 December, 2011, 06:46:00 AST</pubDate>
- <description>
- Cloudy with showers,
- High 7&nbsp;&deg;C, Low 0&nbsp;&deg;C, P.O.P. 70%
- </description>
- </item>
- <item>
- <title>Tuesday, January 3, 2012</title>
- <link>http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=day3#ltermfx</link>
- <guid>http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=day3#ltermfx</guid>
- <pubDate>Fri, 30 December, 2011, 06:46:00 AST</pubDate>
- <description>
- Variable cloudiness,
- High -2&nbsp;&deg;C, Low -8&nbsp;&deg;C, P.O.P. 20%
- </description>
- </item>
- </channel>
- </rss>
- ---------------------------
- <item>
- <title>Sunday, January 1, 2012</title>
- <guid>http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=day1#ltermfx</guid>
- <link>http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=day1#ltermfx</link>
- <pubDate>Fri, 30 December, 2011, 06:46:00 AST</pubDate>
- <description>
- Light rain,
- High 3&nbsp;&deg;C, Low 0&nbsp;&deg;C, P.O.P. 90%
- </description>
- ---------------------------
- Light rain,
- High 3&nbsp;&deg;C, Low 0&nbsp;&deg;C, P.O.P. 90%
- </description>
- ---------------------------
- Light rain,
- High 3&nbsp;&deg;C, Low 0&nbsp;&deg;C, P.O.P. 90%
- </description>
- ---------------------------
- Light rain,
- ---------------------------
- Light rain
- ---------------------------
- Light rain
- ---------------------------
- Light rain
- >>>

