2015年9月6日 星期日
Python-如何利用Gmail發信
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
def sendGmailSmtp(strGmailUser,strGmailPassword,strRecipient,strSubject,strContent):
strMessage = MIMEMultipart()
strMessage['From'] = strGmailUser
strMessage['To'] = strRecipient
strMessage['Subject'] = strSubject
strMessage.attach(MIMEText(strContent))
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(strGmailUser, strGmailPassword)
mailServer.sendmail(strGmailUser, strRecipient, strMessage.as_string())
mailServer.close()
return 'send successed'
print sendGmailSmtp('寄件人@gmail.com','Gmail應用程式密碼','收件人@gmail.com','subject','Gino Test')
Ps, 記得先至Gmail申請應用程式密碼,才能順利發信。
Python-字串處理語法
[Substring]
sTmp='2015-09-05'
sYY=sTmp[0:4]
sMM=sTmp[5:-3]
sDD=sTmp[8:]
[Trim]
左trim
info[0].lstrip()
右trim
info[0].rstrip()
[Replace]
tmpData = tmpData.replace('=','')
tmpData = tmpData.replace('\""','')
tmpData = tmpData.replace(' ','')
sTmp='2015-09-05'
sYY=sTmp[0:4]
sMM=sTmp[5:-3]
sDD=sTmp[8:]
[Trim]
左trim
info[0].lstrip()
右trim
info[0].rstrip()
[Replace]
tmpData = tmpData.replace('=','')
tmpData = tmpData.replace('\""','')
tmpData = tmpData.replace(' ','')
2015年8月30日 星期日
python - 日期迴圈
在網路上撈取歷史資料網站時,時常要下時間條件,因此常用到時間迴圈
import datetime as dt
startdate = dt.datetime(2015, 8,1)
end_ate = dt.datetime(2015, 12,31)
totaldays = (enddate - startdate).days + 1
#在for迴圈內作業。
for daynumber in range(totaldays):
datestring = (startdate + dt.timedelta(days = daynumber)).date()
#print datestring.strftime("%Y%m%d")
import datetime as dt
startdate = dt.datetime(2015, 8,1)
end_ate = dt.datetime(2015, 12,31)
totaldays = (enddate - startdate).days + 1
#在for迴圈內作業。
for daynumber in range(totaldays):
datestring = (startdate + dt.timedelta(days = daynumber)).date()
#print datestring.strftime("%Y%m%d")
Python - 安裝步驟 for Win7
1.先在windows環境安裝python-2.7.8.msi
(下載位置: https://www.python.org/download/releases/2.7.8/)
2.把get-pip.py
放置於C:\Python27\目錄下
執行: python get-pip.py
(下載位置: https://pip.pypa.io/en/latest/installing.html)
3.安裝pymssql
c:\Python27\Scripts\pip install pymysql
4.安裝 beautifulsoup4
c:\Python27\Scripts\easy_install beautifulsoup4
5.安裝 requests
C:\Python27\Scripts>pip install requests
(下載位置: https://www.python.org/download/releases/2.7.8/)
2.把get-pip.py
放置於C:\Python27\目錄下
執行: python get-pip.py
(下載位置: https://pip.pypa.io/en/latest/installing.html)
3.安裝pymssql
c:\Python27\Scripts\pip install pymysql
4.安裝 beautifulsoup4
c:\Python27\Scripts\easy_install beautifulsoup4
5.安裝 requests
C:\Python27\Scripts>pip install requests
訂閱:
文章 (Atom)