- 论坛徽章:
- 4
|
1.你是如何学习Python的?看书?看视频?还是?
买了一本python学习手册第4版,看了俩月,略有入门。其他就是根据自己的需求在网上搜相关的资源了。
2.你工作中有用到Python吗?主要用来做什么?
python主要是抓取cacti的监控图,因为每周要把监控图汇总成文档。
3.你觉得Python跟其他脚本语言比优势在哪?
python相对于shell来讲,可用的库更多,更加的灵活
4.谈谈Python在自动化运维中的应用。
可以利用python进行批处理,对大数据也有帮助
5.给出一个你自己写的Python脚本(最好有详细注释)
当时为朋友写的一个抓菜谱的程序,url可以改成批量,写的不好,见谅,求星辰大神赐书!- #encoding=utf-8
- import urllib2
- import re
- import smtplib
- from email.mime.text import MIMEText
- from email.mime.multipart import MIMEMultipart
- ''''A=[]
- B=[]
- page='您访问的页面不存在'''
- '''url='http://home.meishichina.com/recipe-192222.html'''
- def menu(url):
- A=[]
- B=[]
- page='您访问的页面不存在'
- try:
- headers={'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
- res=urllib2.Request(url,headers=headers)
- response=urllib2.urlopen(res).read()
- #print response
- #Python正则匹配中文
- ############################
- context=re.search(ur"[\u4e00-\u9fa5]+",response.decode('utf-8'))#Python正则匹配中文
-
- #print context.group(0)
- #############################
- if context.group(0)==page:
- print 'Page not found!'
- #<h2 class="recipe_De_title"><a href="#" id="recipe_title">烤孜然排骨</a></h2>
- else :
- resultname=re.findall(ur'<h2 class="recipe_De_title"><a href="#" id="recipe_title">([\u4e00-\u9fa5]+)</a></h2>',response.decode('utf-8'))
- for i in resultname:
- name=i.encode('utf-8')
- name=name.encode('cp936')
-
-
- resultcontent=re.findall(ur'<li><span class="category_s1">([\u4e00-\u9fa5]+)</span><span class="category_s2">\(.*\)</span></li>',response.decode('utf-8'))
- for m in resultcontent:
- content=m.encode('utf-8')
- A.append(m.encode('utf-8'))
-
-
- resultcook=re.findall(ur'<li><span class="category_s1">([\u4e00-\u9fa5]+)</span></li>',response.decode('utf-8'))
- for v in resultcook:
- cook=v.encode('utf-8')
-
- resultstep=re.findall(ur'<div class="recipeStep_word">(.*)</div>',response)
- for x in resultstep:
- B.append(x.encode('utf-8'))
-
-
-
- file=open('D:\\Program\\menu\\'+name+'.txt','w')
- n=0
- file.write('食材\n'+'-'*10+'\n')
- while n<len(A):
-
- file.write(A[n]+'\n')
- n+=1
- else:
- file.write("-"*10+"\n厨具\n")
- file.write(cook)
- file.write('\n'+'-'*10+'\n')
- #file.write("-"*10+"\n制作步骤\n")
-
-
-
-
-
- m=0
- while m<len(resultstep):
- file.write(B[m]+'\n')
- m+=1
- else:
- file.close()
- except urllib2.HTTPError as e:
- print e.code
-
- def sendmail(to_list,sub,result):
- headers={'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
- res=urllib2.Request(url,headers=headers)
- response=urllib2.urlopen(res).read()
- resultname1=re.findall(ur'<h2 class="recipe_De_title"><a href="#" id="recipe_title">([\u4e00-\u9fa5]+)</a></h2>',response.decode('utf-8'))
- for i in resultname1:
- name=i.encode('utf-8')
- name=name.encode('cp936')
-
- me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
- '''msg = MIMEText(result)'''
- #创建一个带附件的实例
- msg = MIMEMultipart()
- #构造附件1
- att1 = MIMEText(open('D:\\Program\\menu\\'+name+'.txt', 'rb').read(), 'base64', 'gb2312')
- att1["Content-Type"] = 'application/octet-stream'
- #att1["Content-Disposition"] = 'attachment; filename="stock.txt"'#这里的filename可以任意写,写什么名字,邮件中显示什么名字
- att1["Content-Disposition"] = 'attachment; filename="menu.txt"'
- msg.attach(att1)
-
- msg['Subject'] = sub
- msg['From'] = me
- msg['To'] = ";".join(to_list)
- try:
- s = smtplib.SMTP()
- s.connect(mail_host)
- s.login(mail_user,mail_pass)
- s.sendmail(me, to_list, msg.as_string())
- s.close()
- return True
- except Exception, e:
- print str(e)
- return False
- if __name__=='__main__':
- url='http://home.meishichina.com/recipe-192222.html'
- result=menu(url)
-
- mailto_list=["。。。。@qq.com",'。。。。@qq.com']
- #####################
- mail_host="smtp.126.com"
- mail_user="。。。"
- mail_pass="。。。"
- mail_postfix="126.com"
- sendmail( mailto_list,"menu",result)
复制代码 |
|