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

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

From Dr Shin Spine Clinic

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
-
import re
+
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>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)
-
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('보고 싶은 첫번째 게시판의 리스트화면')
+
response = ClientCookie.urlopen('보고 싶은 첫번째 게시판의 리스트화면')<br>html = response.read()<br>tellUrlList = re.findall("보고 싶은 게시판에서 따온 리스트[^']+", html)<br>targeturl = "보고 싶은 게시판의 마스터 주소"+tellUrlList[0]<br>print targeturl
-
html = response.read()
+
-
tellUrlList = re.findall("보고 싶은 게시판에서 따온 리스트[^']+", html)
+
-
targeturl = "보고 싶은 게시판의 마스터 주소"+tellUrlList[0]
+
-
print targeturl
+
-
contents_1 = ClientCookie.urlopen(targeturl).read()    
+
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
-
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
contents_1 = titleMod_1+ '\n' + textMod5
-
response = ClientCookie.urlopen('보고싶은 두번째 게시판의 리스트화면')
+
response = ClientCookie.urlopen('보고싶은 두번째 게시판의 리스트화면')<br>html = response.read()<br>tellUrlList = re.findall("보고 싶은 두번째 게시판에서 따온 리스트[^']+", html)<br>targeturl = "http://www.gyeseong1882.es.kr/"+tellUrlList[0]<br>print targeturl
-
html = response.read()
+
-
tellUrlList = re.findall("보고 싶은 두번째 게시판에서 따온 리스트[^']+", html)
+
-
targeturl = "http://www.gyeseong1882.es.kr/"+tellUrlList[0]
+
-
print targeturl
+
-
contents_2 = ClientCookie.urlopen(targeturl).read()    
+
contents_2 = ClientCookie.urlopen(targeturl).read() <br>soup = BeautifulSoup(contents_2)<br>title = str(soup.span)<br>titleMod = title[20:-11]<br>textMod0 = str (soup.findAll ('span', attrs={'lang':'EN-US'}))<br>textMod1 = textMod0.replace('&lt;span style="mso-fareast-font-family: 바탕" lang="EN-US"&gt;', "")<br>textMod2 = textMod1.replace("&lt;/span&gt;", "")<br>textMod3 = textMod2.replace("[", "")<br>textMod4 = textMod3.replace("&amp;nbsp;", "")<br>textMod5 = textMod4.replace("]", "")<br>print textMod5
-
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
contents = contents_1+'\n'+'\n'+titleMod+ '\n' + textMod5
-
sender = '내주소'
+
sender = '내주소'<br>recipients = '마눌 주소'<br>cc_addresses =['나도 보고 싶으면 넣는 주소']<br>msg = MIMEText(contents)<br>msg['Subject'] = titleMod_1<br>msg['From'] = sender<br>msg['To'] = recipients<br>msg['Cc'] = ', '.join(cc_addresses)<br>smtpserver = 'SMTP서버 주소'<br>smtpuser = 'SMTP접속 아이디' # set SMTP username here<br>smtppass = 'SMTP접속 비밀번호' # set SMTP password here<br>session = smtplib.SMTP("SMTP서버주소", 25)<br>session.ehlo()<br>session.starttls()<br>session.ehlo()<br>session.login(smtpuser, smtppass)<br>toaddrs = [recipients] + cc_addresses<br>smtpresult = session.sendmail(sender, toaddrs, msg.as_string())<br>if smtpresult:<br>errstr = ""<br>for recip in smtpresult.keys():<br>errstr = """Could not delivery mail to: %s<br>Server said: %s<br>%s<br>%s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)<br>raise smtplib.SMTPException, errstr<br>session.close()<br>
-
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()
+

Revision as of 03:22, 11 February 2012

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 = "http://www.gyeseong1882.es.kr/"+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()