Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1093108
  • 博文数量: 110
  • 博客积分: 10068
  • 博客等级: 上将
  • 技术积分: 1505
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-25 11:28
文章分类

全部博文(110)

文章存档

2011年(4)

2010年(5)

2009年(73)

2008年(28)

我的朋友

分类: Python/Ruby

2011-12-31 08:37:09

从网站返回的一个XML文件中截取某个字符串
源码如下:
  1. #!/usr/bin/python

  2. '''
  3. 此例对一个XML文件(str类型)进行分割取值,'list'类型不能进行split操作;取值超过1个时,即变成list类型。
  4. weather.split('</item>')[1]先用“”分割,取得第二个元素。split('<description>')[1]
  5. 再用'<description>'分割这个元素,取得分割后的第二个元素,依次类推
  6. strip():去掉空格
  7. strip('\n'):去掉换行符
  8. replace('    ', ''):替换掉多余空格
  9. '''

  10. import urllib2
  11. weather = urllib2.urlopen('').read()

  12. next_weather1 = weather.split('')[1]
  13. next_weather2 = weather.split('')[1].split('')[1]
  14. next_weather2_1 = weather.split('')[1].split('')[0:1]
  15. next_weather3 = weather.split('')[1].split('')[1].strip('\n')
  16. next_weather4 = weather.split('')[1].split('')[1].strip('\n').split('\n')[1]
  17. next_weather5 = weather.split('')[1].split('')[1].strip('\n').split('\n')[1].split(',')[0]
  18. next_weather6 = weather.split('')[1].split('')[1].strip('\n').split('\n')[1].split(',')[0].replace('    ', '')
  19. next_weather7 = weather.split('')[1].split('')[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.  

  4.     The Weather Network - Charlottetown, Prince Edward Island

  5.     http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs

  6.     The Weather Network - Canada's number one source for weather information in both the cable and web spaces.

  7. Copyright The Weather Network. Use of this RSS feed is subject to the Terms of Use as defined at

  8.     en-us


  9. http://www.theweathernetwork.com/common/images/rss/rss_logo_en.jpg

  10. The Weather Network

  11. http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs


  12. http://blogs.law.harvard.edu/tech/rss


  13.    

  14.       Current Weather

  15.  http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs

  16.  http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs

  17.       Fri, 30 Dec 2011, 08:00:00 AST

  18.  


  19. spacer

  20. http://www.theweathernetwork.com/index.php?product=weather&placecode=cape0005&ref=current_obs


  21.       Clear,

  22.  

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

  24. calm


  25.    




  26.    

  27.       Sunday, January 1, 2012

  28.    

  29.    

  30.  Fri, 30 December, 2011, 06:46:00 AST

  31.      

  32. Light rain,

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

  34.  

  35.    



  36.    

  37.       Monday, January 2, 2012

  38.    


  39.  Fri, 30 December, 2011, 06:46:00 AST

  40.        

  41. Cloudy with showers,

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

  43.  

  44.    



  45.      

  46.       Tuesday, January 3, 2012

  47.    


  48.  Fri, 30 December, 2011, 06:46:00 AST

  49.      

  50. Variable cloudiness,

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

  52.  

  53.    

  54.      



  55. ---------------------------





  56.    

  57.       Sunday, January 1, 2012

  58.    

  59.    

  60.  Fri, 30 December, 2011, 06:46:00 AST

  61.      

  62. Light rain,

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

  64.  

  65.     
  66. ---------------------------


  67. Light rain,

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

  69.  

  70.     
  71. ---------------------------


  72. Light rain,

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

  74.  

  75.     
  76. ---------------------------
  77. Light rain,

  78. ---------------------------
  79. Light rain
  80. ---------------------------
  81. Light rain
  82. ---------------------------
  83. Light rain
  84. >>> 


阅读(25969) | 评论(0) | 转发(1) |
0

上一篇:Python打印当前目录下的图片文件

下一篇:没有了

给主人留下些什么吧!~~