免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: yestreenstars
打印 上一主题 下一主题

[其他] 学习与分享——Python自动化运维 [复制链接]

论坛徽章:
0
1 [报告]
发表于 2014-11-26 16:30 |显示全部楼层
  1. 1.你是如何学习Python的?看书?看视频?还是?
  2. 看书:python语法是从python简明手册上看的。python标准库
  3. 同事指导:有幸在北京的公司时候一个同事指导过我写一些简单的py脚本

  4. 2.你工作中有用到Python吗?主要用来做什么?
  5. 工作中用到python的地方还是蛮多的:
  6. a. 我们用aws的时候,一些自动部署、检测脚本都是用python写的
  7. b. 平时一些备份任务现在也改成python去做
  8. c. 帮助运营人员去分析一些数据(mysql、DB的一些操作)

  9. 3.你觉得Python跟其他脚本语言比优势在哪?
  10. 可读性很强、可移植性强

  11. 4.谈谈Python在自动化运维中的应用。
  12. 现在我们生产环境使用ansible做为自动化部署和配置管理,配置git,真的很赞

  13. 5.给出一个你自己写的Python脚本(最好有详细注释)。
  14. 第一次写爬虫的时候脚本:
  15. import os
  16. import urllib
  17. import re

  18. # 定义下载路径
  19. download_path = os.path.dirname(os.path.abspath(__file__))


  20. class spider(object):
  21.     def __init__(self, url):
  22.         self.url = url

  23.     # 分析内容   
  24.     def parse(self,content):
  25.         pattern = 'src="(http://.*\.jpg)\s*"'
  26.         matchs = re.findall(pattern,content,re.M)
  27.         return matchs

  28.     # 下载图片
  29.     def downloads(self,urls):
  30.         d_path = download_path + "/test"
  31.         if not os.path.exists(d_path):
  32.             os.mkdir(d_path)
  33.         for url in urls:
  34.             filename = url.split("/")[-1]
  35.             print url
  36.             print "Downloads %s" % (filename)
  37.             output = "%s/%s" % (d_path, filename)
  38.             urllib.urlretrieve(url,output)
  39.      
  40.     #  run主体      
  41.     def run(self):
  42.         d_url = self.url
  43.         fd = urllib.urlopen(d_url)
  44.         try:
  45.             content = fd.read()
  46.             urls = self.parse(content)
  47.             self.downloads(urls)
  48.         finally:
  49.             fd.close()

  50. if __name__ == "__main__":
  51.     sp = spider("http://eladies.sina.com.cn/photo/")
  52.     sp.run()
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP