Python으로 작성한 아들 학교에서 알림장 및 공지사항 퍼다 마눌 메일로 보내주기

Python으로 작성한 아들 학교에서 알림장 및 공지사항 퍼다 마눌 메일로 보내주기

From Dr Shin Spine Clinic

Jump to: navigation, search

리치에디터 편집기의 한계로 정확히 넣지는 못했습니다. 참고만 하세요. 전 요걸 bbfreeze로 실행 파일로 만들고 제 xp컴퓨터에 예정된 작업으로 올려 매일 2시와 8시에 실행하게 해두었습니다. ㅋㅋ


import re
import urllib
import urllib2
import ClientCookie
from ClientForm import ParseResponse
from BeautifulSoup import BeautifulSoup
import smtplib
from email.mime.text import MIMEText
import email
import email.mime.text
import email.mime.base
import email.mime.multipart
import email.iterators
import email.generator
import email.utils
try:
from email.MIMEText import MIMEText
except:
from email.mime import text as MIMEText

forms = ParseResponse(urllib2.urlopen('보고싶은 첫번째 게시판의 첫화면'))
form = forms[1]
form['mb_id'] = '게시판 접속 아이디'
form['mb_passwd'] = '게시판 접속 비밀번호'
request = form.click()
response = ClientCookie.urlopen(request)

response = ClientCookie.urlopen('보고 싶은 첫번째 게시판의 리스트화면')
html = response.read()
tellUrlList = re.findall("보고 싶은 게시판에서 따온 리스트[^']+", html)
targeturl = "보고 싶은 게시판의 마스터 주소"+tellUrlList[0]
print targeturl

contents_1 = ClientCookie.urlopen(targeturl).read()
soup = BeautifulSoup(contents_1)
title = str(soup.span)
titleMod_1 = title[20:-11]
textMod0 = str (soup.findAll ('div'))
textMod1 = textMod0.replace("<div>", "")
textMod2 = textMod1.replace("</div>", "")
textMod3 = textMod2.replace("[", "")
textMod4 = textMod3.replace("&nbsp;", "")
textMod5 = textMod4.replace("]", "")
print textMod5

contents_1 = titleMod_1+ '\n' + textMod5

response = ClientCookie.urlopen('보고싶은 두번째 게시판의 리스트화면')
html = response.read()
tellUrlList = re.findall("보고 싶은 두번째 게시판에서 따온 리스트[^']+", html)
targeturl = "보고 싶은 게시판의 마스터 주소/"+tellUrlList[0]
print targeturl

contents_2 = ClientCookie.urlopen(targeturl).read()
soup = BeautifulSoup(contents_2)
title = str(soup.span)
titleMod = title[20:-11]
textMod0 = str (soup.findAll ('span', attrs={'lang':'EN-US'}))
textMod1 = textMod0.replace('<span style="mso-fareast-font-family: 바탕" lang="EN-US">', "")
textMod2 = textMod1.replace("</span>", "")
textMod3 = textMod2.replace("[", "")
textMod4 = textMod3.replace("&nbsp;", "")
textMod5 = textMod4.replace("]", "")
print textMod5

contents = contents_1+'\n'+'\n'+titleMod+ '\n' + textMod5

sender = '내주소'
recipients = '마눌 주소'
cc_addresses =['나도 보고 싶으면 넣는 주소']
msg = MIMEText(contents)
msg['Subject'] = titleMod_1
msg['From'] = sender
msg['To'] = recipients
msg['Cc'] = ', '.join(cc_addresses)
smtpserver = 'SMTP서버 주소'
smtpuser = 'SMTP접속 아이디' # set SMTP username here
smtppass = 'SMTP접속 비밀번호' # set SMTP password here
session = smtplib.SMTP("SMTP서버주소", 25)
session.ehlo()
session.starttls()
session.ehlo()
session.login(smtpuser, smtppass)
toaddrs = [recipients] + cc_addresses
smtpresult = session.sendmail(sender, toaddrs, msg.as_string())
if smtpresult:
errstr = ""
for recip in smtpresult.keys():
errstr = """Could not delivery mail to: %s
Server said: %s
%s
%s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
raise smtplib.SMTPException, errstr
session.close()