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

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

From Dr Shin Spine Clinic

Revision as of 03:18, 11 February 2012 by Cistern (Talk | contribs)
Jump to: navigation, search
  1. !/usr/bin/env python
  2. -*- coding:utf-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
       
  1. 게시판에 아이디와 비밀번호로 열기

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

  1. 학년 게시판에서 글목록은 리스트로 반환

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

  1. 학년 게시판 첫번째 글의 내용을 파싱

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("
", "") textMod2 = textMod1.replace("
", "")

textMod3 = textMod2.replace("[", "") textMod4 = textMod3.replace(" ", "") textMod5 = textMod4.replace("]", "") print textMod5

  1. 메일에 보낼 내용을 담기

contents_1 = titleMod_1+ '\n' + textMod5

  1. 공지사항 게시판에서 글목록은 리스트로 반환

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

  1. 공지사항 게시판 첫번째 글의 내용을 파싱

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('', "") textMod2 = textMod1.replace("", "") textMod3 = textMod2.replace("[", "") textMod4 = textMod3.replace(" ", "") textMod5 = textMod4.replace("]", "") print textMod5

  1. 메일에 보낼 내용을 모아 담기

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

  1. 구글 SMTP를 이용하여 메일로 보내기

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()