日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2023/12/9 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

def extract_url(info): #reg get url&titlerege = "<li><a href=\"(.*)\" target=_blank>"url = re.findall(rege,info)return urldef extract_title(info): pat = "\" target=_blank>(.*)</a><span class="title = re.findall(pat,info)return title

就可以得到新聞的URL和標題,以列表形式返回

?

?

3.排版
整理獲得的信息,把信息排版成適合網頁瀏覽的形式
我是把這些信息放到一個表格里

mail_context = "" mail_context += "<table width=\"700\" border=\"1\" align=\"left\" face=\"宋體\">" for i in range(0,n):mail_context += "<tr><td><font size=\"2\">"mail_context += "<span class="line1"><a href=\""mail_context += url[i]mail_context += "\" target=_blank>"mail_context += title[i]mail_context += "</a></span>"mail_context += "</font></td>" mail_context += "</table>"

?

?

4.獲取excel中的郵件列表

def get_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.發送郵件
用自帶的模塊來發送,先登錄到一個郵箱的服務器上,然后通過該郵箱向目的郵箱發送郵件

import smtplib from email.mime.text import MIMEText from email.MIMEMultipart import MIMEMultipart from email.Header import Headerdef sendsimplemail (text,dest):msg = MIMEText(text,'html','gb2312')#html formatmsg['Subject'] = Header('title', 'gb2312')msg['From'] = 'sourcedest'msg['To'] = str(dest)try:#login mail serversmtp = smtplib.SMTP()smtp.connect(r'smtp.*******')#mail serversmtp.login('username', 'password')#send mailsmtp.sendmail('sender', dest, msg.as_string())smtp.close()except Exception, e:print e

?

?

6.設置為開機自啟動
以下是轉的http://apps.hi.baidu.com/share/detail/22013974
下載python的win32支持。我使用的是:pywin32-202.win32-py2.3.exe安裝好后就可以來寫我們的服務了。
我們先來建立一個空的服務,建立test1.py這個文件,并寫入如下代碼:

# -*- coding: cp936 -*- import win32serviceutil import win32service import win32event class test1(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) def SvcStop(self): # 先告訴SCM停止這個過程 self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) # 設置事件 win32event.SetEvent(self.hWaitStop) def SvcDoRun(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這個服務。

這樣每次開機之后都可以收到一封郵件了

?

轉載于:https://www.cnblogs.com/w0w0/archive/2012/05/02/2478639.html

總結

以上是生活随笔為你收集整理的python实现抓取网页上的内容并发送到邮箱的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。