python爬取邮件内容_python实现抓取网页上的内容并发送到邮箱

要达到的目的:

从特定网页中抓取信息,排版后发送到邮箱中

关键点:

下载网页,从网页里抓取出需要的信息

HTML排版

发送到指定邮箱

实现:

1.python下载网页

直接用库函数就可以实现

from urllib import urlretrieve

from urllib import urlopen

doc = urlopen("http://roll.tech.sina.com.cn/tele/2012-05-01.shtml").read()

以获取新浪网5.1电信滚动新闻为例

doc就是整个网页的内容

2.用正则表达式提取新闻标题和url

defextract_url(info):#reg get url&title

rege = "

"url=re.findall(rege,info)returnurldefextract_title(info):

pat= "\" target=_blank>(.*)

"

for i inrange(0,n):

mail_context+= "

"mail_context+= "

mail_context+= "\" target=_blank>"mail_context+=title[i]

mail_context+= "

"mail_context+= ""mail_context+= ""

4.获取excel中的邮件列表

defget_email_list():

path=os.getcwd()

wb= open_workbook(path+"\\email_list.xls")

sheet=wb.sheet_by_name("email")

first_column=sheet.col_values(0)return first_column

5.发送邮件

用自带的模块来发送,先登录到一个邮箱的服务器上,然后通过该邮箱向目的邮箱发送邮件

importsmtplibfrom email.mime.text importMIMETextfrom email.MIMEMultipart importMIMEMultipartfrom email.Header importHeaderdefsendsimplemail (text,dest):

msg= MIMEText(text,'html','gb2312')#html format

msg['Subject'] = Header('title', 'gb2312')

msg['From'] = 'sourcedest'msg['To'] =str(dest)try:#login mail server

smtp =smtplib.SMTP()

smtp.connect(r'smtp.*******')#mail server

smtp.login('username', 'password')#send mail

smtp.sendmail('sender', dest, msg.as_string())

smtp.close()exceptException, e:print e

6.设置为开机自启动

以下是转的http://apps.hi.baidu.com/share/detail/22013974

下载python的win32支持。我使用的是:pywin32-202.win32-py2.3.exe安装好后就可以来写我们的服务了。

我们先来建立一个空的服务,建立test1.py这个文件,并写入如下代码:

#-*- coding: cp936 -*-

importwin32serviceutilimportwin32serviceimportwin32eventclasstest1(win32serviceutil.ServiceFramework):

_svc_name_= "test_python"_svc_display_name_= "test_python"

def __init__(self, args):

win32serviceutil.ServiceFramework.__init__(self, args)#把需要的操作都放在这里

self.hWaitStop=win32event.CreateEvent(None, 0, 0, None)defSvcStop(self):#先告诉SCM停止这个过程

self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)#设置事件

win32event.SetEvent(self.hWaitStop)defSvcDoRun(self):#等待服务被停止

win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)if __name__=='__main__':

win32serviceutil.HandleCommandLine(test1)

这里注意,如果你需要更改文件名,比如将win32serviceutil.HandleCommandLine(test1)中的test1更改为你的文件名,同时class也需要和你的文件名一致,否则会出现服务不能启动的问题。

在命令窗口执行,test1.py可以看到帮助提示

C:\>test1.py

Usage: 'test1.py [options] install|update|remove|start [...]|stop|restart [...]|

debug [...]'

Options for 'install' and 'update' commands only:

--username domain\username : The Username the service is to run under

--password password : The password for the username

--startup [manual|auto|disabled] : How the service starts, default = manual

--interactive : Allow the service to interact with the desktop. (for my auto_desktop.py, this option is needed)

C:\>

安装我们的服务

[code:1:05b7353f1c]C:\>test1.py install

Installing service test_python to Python class C:\test1.test1

Service installed

C:\>

我们就可以用命令或者在控制面板-》管理工具-》服务中管理我们的服务了。在服务里面可以看到test_python这个服务。

这样每次开机之后都可以收到一封邮件了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值