·¢²©ÎÄ
Abner Kou

abner.blog.chinaunix.net

¿ÜÖù(Abner)µÄ¸öÈ˲©¿Í   
¸öÈË×ÊÁÏ
  • ²©¿Í·ÃÎÊ£º412871
  • ²©ÎÄÊýÁ¿£º150
  • ²©¿Í»ý·Ö£º10087
  • ²©¿ÍµÈ¼¶£ºÉϽ«
  • ¹Ø×¢ÈËÆø£º 3
  • ×¢²áʱ¼ä£º2007-04-25 11:28:59
¶©ÔÄÎҵIJ©¿Í
  • ¶©ÔÄ
  • ¶©Ôĵ½Ïʹû
  • ¶©Ôĵ½×¥Ïº
  • ¶©Ôĵ½Google
×ÖÌå´óС£º´ó ÖРС²©ÎÄ
PythonÖеÄsplit()Ó¦Óà (2011-12-31 08:37)
±êÇ©hidden  ·ÖÀࣺ Python


´ÓÍøÕ¾·µ»ØµÄÒ»¸öXMLÎļþÖнØÈ¡Ä³¸ö×Ö·û´®
Ô´ÂëÈçÏ£º
  1. #!/usr/bin/python

  2. '''
  3. ´ËÀý¶ÔÒ»¸öXMLÎļþ£¨strÀàÐÍ£©½øÐзָîȡֵ£¬'list'ÀàÐͲ»ÄܽøÐÐsplit²Ù×÷£»È¡Öµ³¬¹ý1¸öʱ£¬¼´±ä³ÉlistÀàÐÍ¡£
  4. weather.split('</item>')[1]ÏÈÓá°</item>¡±·Ö¸î£¬È¡µÃµÚ¶þ¸öÔªËØ¡£split('<description>')[1]
  5. ÔÙÓÃ'<description>'·Ö¸îÕâ¸öÔªËØ£¬È¡µÃ·Ö¸îºóµÄµÚ¶þ¸öÔªËØ£¬ÒÀ´ÎÀàÍÆ
  6. strip()£ºÈ¥µô¿Õ¸ñ
  7. strip('\n'):È¥µô»»Ðзû
  8. replace('    ', ''):Ìæ»»µô¶àÓà¿Õ¸ñ
  9. '''

  10. import urllib2
  11. weather = urllib2.urlopen('http://rss.theweathernetwork.com/weather/cape0005').read()

  12. next_weather1 = weather.split('</item>')[1]
  13. next_weather2 = weather.split('</item>')[1].split('<description>')[1]
  14. next_weather2_1 = weather.split('</item>')[1].split('<description>')[0:1]
  15. next_weather3 = weather.split('</item>')[1].split('<description>')[1].strip('\n')
  16. next_weather4 = weather.split('</item>')[1].split('<description>')[1].strip('\n').split('\n')[1]
  17. next_weather5 = weather.split('</item>')[1].split('<description>')[1].strip('\n').split('\n')[1].split(',')[0]
  18. next_weather6 = weather.split('</item>')[1].split('<description>')[1].strip('\n').split('\n')[1].split(',')[0].replace('    ', '')
  19. next_weather7 = weather.split('</item>')[1].split('<description>')[1].strip('\n').split('\n')[1].split(',')[0].strip()

  20. print type(weather)
  21. print weather
  22. print ('---------------------------')
  23. print next_weather1
  24. print ('---------------------------')
  25. print next_weather2
  26. print ('---------------------------')
  27. #print next_weather2_1
  28. #print type(next_weather2_1)
  29. #print ('---------------------------')
  30. print next_weather3
  31. print ('---------------------------')
  32. print next_weather4
  33. print ('---------------------------')
  34. print next_weather5
  35. print ('---------------------------')
  36. print next_weather6
  37. print ('---------------------------')
  38. print next_weather7
ÔËÐнá¹û
  1. >>> ================================ RESTART ================================
  2. >>> 
  3. <type 'str'>
  4. <?xml version="1.0" encoding="ISO-8859-1"?><rss version="2.0">

  5.   <channel>

  6.     <title>The Weather Network - Charlottetown, Prince Edward Island</title>

  7.     <link>http://www.theweathernetwork.com/index.php?product=weather&amp;placecode=cape0005&amp;ref=current_obs</link>

  8.     <description>The Weather Network - Canada's number one source for weather information in both the cable and web spaces. </description>

  9. <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>

  10.     <language>en-us</language>

  11. <image>

  12. <url>http://www.theweathernetwork.com/common/images/rss/rss_logo_en.jpg</url>

  13. <title>The Weather Network</title>

  14. <link>http://www.theweathernetwork.com/index.php?product=weather&amp;placecode=cape0005&amp;ref=current_obs</link>

  15. </image>

  16. <docs>http://blogs.law.harvard.edu/tech/rss</docs>


  17.     <item>

  18.       <title>Current Weather</title>

  19.  <guid>http://www.theweathernetwork.com/index.php?product=weather&amp;placecode=cape0005&amp;ref=current_obs</guid>

  20.  <link>http://www.theweathernetwork.com/index.php?product=weather&amp;placecode=cape0005&amp;ref=current_obs</link>

  21.       <pubDate>Fri, 30 Dec 2011, 08:00:00 AST</pubDate>

  22.  <image>

  23. <url>http://rss.counter.weather.ca/counter.gif</url>

  24. <title>spacer</title>

  25. <link>http://www.theweathernetwork.com/index.php?product=weather&amp;placecode=cape0005&amp;ref=current_obs</link>

  26. </image>

  27.       <description>Clear,

  28.  

  29. -10&amp;nbsp;&amp;deg;C , Humidity 79% , Wind 

  30. calm </description>


  31.     </item>




  32.     <item>

  33.       <title>Sunday, January 1, 2012</title>

  34.    <guid>http://www.theweathernetwork.com/index.php?product=weather&amp;placecode=cape0005&amp;ref=day1#ltermfx</guid>

  35.    <link>http://www.theweathernetwork.com/index.php?product=weather&amp;placecode=cape0005&amp;ref=day1#ltermfx</link>

  36.  <pubDate>Fri, 30 December, 2011, 06:46:00 AST</pubDate>

  37.       <description>

  38. Light rain,

  39. High  3&amp;nbsp;&amp;deg;C, Low  0&amp;nbsp;&amp;deg;C, P.O.P.  90%  

  40.  </description>

  41.     </item>



  42.    <item>

  43.       <title>Monday, January 2, 2012</title>

  44.    <link>http://www.theweathernetwork.com/index.php?product=weather&amp;placecode=cape0005&amp;ref=day2#ltermfx</link>

  45. <guid>http://www.theweathernetwork.com/index.php?product=weather&amp;placecode=cape0005&amp;ref=day2#ltermfx</guid>

  46.  <pubDate>Fri, 30 December, 2011, 06:46:00 AST</pubDate>

  47.        <description>

  48. Cloudy with showers,

  49. High 7&amp;nbsp;&amp;deg;C, Low 0&amp;nbsp;&amp;deg;C, P.O.P. 70%  

  50.  </description>

  51.     </item>



  52.      <item>

  53.       <title>Tuesday, January 3, 2012</title>

  54.    <link>http://www.theweathernetwork.com/index.php?product=weather&amp;placecode=cape0005&amp;ref=day3#ltermfx</link>

  55. <guid>http://www.theweathernetwork.com/index.php?product=weather&amp;placecode=cape0005&amp;ref=day3#ltermfx</guid>

  56.  <pubDate>Fri, 30 December, 2011, 06:46:00 AST</pubDate>

  57.       <description>

  58. Variable cloudiness,

  59. High -2&amp;nbsp;&amp;deg;C, Low -8&amp;nbsp;&amp;deg;C, P.O.P. 20%  

  60.  </description>

  61.     </item>

  62.       </channel>

  63. </rss>


  64. ---------------------------





  65.     <item>

  66.       <title>Sunday, January 1, 2012</title>

  67.    <guid>http://www.theweathernetwork.com/index.php?product=weather&amp;placecode=cape0005&amp;ref=day1#ltermfx</guid>

  68.    <link>http://www.theweathernetwork.com/index.php?product=weather&amp;placecode=cape0005&amp;ref=day1#ltermfx</link>

  69.  <pubDate>Fri, 30 December, 2011, 06:46:00 AST</pubDate>

  70.       <description>

  71. Light rain,

  72. High  3&amp;nbsp;&amp;deg;C, Low  0&amp;nbsp;&amp;deg;C, P.O.P.  90%  

  73.  </description>

  74.     
  75. ---------------------------


  76. Light rain,

  77. High  3&amp;nbsp;&amp;deg;C, Low  0&amp;nbsp;&amp;deg;C, P.O.P.  90%  

  78.  </description>

  79.     
  80. ---------------------------


  81. Light rain,

  82. High  3&amp;nbsp;&amp;deg;C, Low  0&amp;nbsp;&amp;deg;C, P.O.P.  90%  

  83.  </description>

  84.     
  85. ---------------------------
  86. Light rain,

  87. ---------------------------
  88. Light rain
  89. ---------------------------
  90. Light rain
  91. ---------------------------
  92. Light rain
  93. >>> 


²©¿ÍÍÆ¼öÎÄÕÂ
Ç×£¬Äú»¹Ã»ÓеǼ,Çë[µÇ¼]»ò[×¢²á]ºóÔÙ½øÐÐÆÀÂÛ