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

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

From Dr Shin Spine Clinic

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
-
#!/usr/bin/env python<br># -*- coding:utf-8 -*-<br>import re<br>import urllib<br>import urllib2<br>import ClientCookie <br>from ClientForm import ParseResponse<br>from BeautifulSoup import BeautifulSoup<br>import smtplib<br>from email.mime.text import MIMEText<br>import email<br>import email.mime.text<br>import email.mime.base<br>import email.mime.multipart<br>import email.iterators<br>import email.generator<br>import email.utils<br>try:<br>from email.MIMEText import MIMEText<br>except:<br>from email.mime import text as MIMEText<br><br>#게시판에 아이디와 비밀번호로 열기<br>forms = ParseResponse(urllib2.urlopen('보고싶은 첫번째 게시판의 첫화면'))<br>form = forms[1] <br>form['mb_id'] = '아이디'<br>form['mb_passwd'] = '비밀번호'<br>request = form.click()<br>response = ClientCookie.urlopen(request)  
+
##!/usr/bin/env python<br># -*- coding:utf-8 -*-<br>import re<br>import urllib<br>import urllib2<br>import ClientCookie <br>from ClientForm import ParseResponse<br>from BeautifulSoup import BeautifulSoup<br>import smtplib<br>from email.mime.text import MIMEText<br>import email<br>import email.mime.text<br>import email.mime.base<br>import email.mime.multipart<br>import email.iterators<br>import email.generator<br>import email.utils<br>try:<br>from email.MIMEText import MIMEText<br>except:<br>from email.mime import text as MIMEText<br><br>#게시판에 아이디와 비밀번호로 열기<br>forms = ParseResponse(urllib2.urlopen('보고싶은 첫번째 게시판의 첫화면'))<br>form = forms[1] <br>form['mb_id'] = '아이디'<br>form['mb_passwd'] = '비밀번호'<br>request = form.click()<br>response = ClientCookie.urlopen(request)  
-
#학년 게시판에서 글목록은 리스트로 반환<br>response = ClientCookie.urlopen('보고 싶은 첫번째 게시판의 리스트화면')<br>html = response.read()<br>tellUrlList = re.findall("보고 싶은 게시판에서 따온 리스트[^']+", html)<br>targeturl = "보고 싶은 게시판의 마스터 주소"+tellUrlList[0]<br>print targeturl  
+
#학년 게시판에서 글목록은 리스트로 반환<br>response = ClientCookie.urlopen('보고 싶은 첫번째 게시판의 리스트화면')<br>html = response.read()<br>tellUrlList = re.findall("보고 싶은 게시판에서 따온 리스트[^']+", html)<br>targeturl = "보고 싶은 게시판의 마스터 주소"+tellUrlList[0]<br>print targeturl
-
#학년 게시판 첫번째 글의 내용을 파싱<br>contents_1 = ClientCookie.urlopen(targeturl).read() <br>soup = BeautifulSoup(contents_1)<br>title = str(soup.span)<br>titleMod_1 = title[20:-11]<br>textMod0 = str (soup.findAll ('div'))<br>textMod1 = textMod0.replace("&lt;div&gt;", "")<br>textMod2 = textMod1.replace("&lt;/div&gt;", "")<br>textMod3 = textMod2.replace("[", "")<br>textMod4 = textMod3.replace("&amp;nbsp;", "")<br>textMod5 = textMod4.replace("]", "")<br>print textMod5  
+
#학년 게시판 첫번째 글의 내용을 파싱<br>contents_1 = ClientCookie.urlopen(targeturl).read() <br>soup = BeautifulSoup(contents_1)<br>title = str(soup.span)<br>titleMod_1 = title[20:-11]<br>textMod0 = str (soup.findAll ('div'))<br>textMod1 = textMod0.replace("&lt;div&gt;", "")<br>textMod2 = textMod1.replace("&lt;/div&gt;", "")<br>textMod3 = textMod2.replace("[", "")<br>textMod4 = textMod3.replace("&amp;nbsp;", "")<br>textMod5 = textMod4.replace("]", "")<br>print textMod5
-
#메일에 보낼 내용을 담기<br>contents_1 = titleMod_1+ '\n' + textMod5  
+
#메일에 보낼 내용을 담기<br>contents_1 = titleMod_1+ '\n' + textMod5
#공지사항 게시판에서 글목록은 리스트로 반환<br>response = ClientCookie.urlopen('보고싶은 두번째 게시판의 리스트화면')<br>html = response.read()<br>tellUrlList = re.findall("보고 싶은 두번째 게시판에서 따온 리스트[^']+", html)<br>targeturl = "http://www.gyeseong1882.es.kr/"+tellUrlList[0]<br>print targeturl
#공지사항 게시판에서 글목록은 리스트로 반환<br>response = ClientCookie.urlopen('보고싶은 두번째 게시판의 리스트화면')<br>html = response.read()<br>tellUrlList = re.findall("보고 싶은 두번째 게시판에서 따온 리스트[^']+", html)<br>targeturl = "http://www.gyeseong1882.es.kr/"+tellUrlList[0]<br>print targeturl

Revision as of 03:20, 11 February 2012

    1. !/usr/bin/env python
      # -*- 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

      #게시판에 아이디와 비밀번호로 열기
      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("<div>", "")
    textMod2 = textMod1.replace("</div>", "")
    textMod3 = textMod2.replace("[", "")
    textMod4 = textMod3.replace("&nbsp;", "")
    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('<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
  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()