Python crawl 20 popular items and mail distribution +wxpython simple Gui+py2app to run files

Source: Internet
Author: User

Did you learn python for a while and do something interesting for me? It's a good climb. Crawl to the content, the message is distributed out.

Then ate two days of Wxpython, did a Simple mail management interface, can add or delete mail here, and a key crawler to send.

Finally, simply package it into an app. and tried a py2app. Easy to use.

Because it is for users to join and delete their own mailboxes. So the program must take into account a variety of situations: for example, the input mailbox format is not legal. The input mailbox includes Chinese characters, the delimiter is not correct, all mailboxes are deleted and the email is sent again.



The first is qiubai.py: crawlers, which match what we want. It then returns the content as a little processing.

#!/usr/bin/env python#-*-coding:utf-8-*-import reimport urllib2__author__ = ' Sophie2805 ' class Fetchdata (object): De F __init__ (self,url,referurl,pattern_1,pattern_2): self. url = URL Self.pattern_1 = pattern_1 self.patttern_2 = pattern_2 Self.referurl = Referurl def geth Tml (self): headers = {' user-agent ': ' mozilla/5.0 (Windows; U Windows NT 6.1; En-us;        rv:1.9.1.6) gecko/20091201 firefox/3.5.6 ', ' Referer ': Self.referurl #http://www.qiushibaike.com/} req = Urllib2. Request (url = self. URL, #http://www.qiushibaike.com/text headers = headers) return Urllib2.urlopen (req). Read () def GETDA Ta (self,html): P_1 = Re.compile (self.pattern_1) div_content = P_1.findall (Html.decode (' UTF8 ')) row =            0 for M in range (len (div_content)): div_content[m] = re.sub (Self.patttern_2, ", Div_content[m]) DIV_CONTENT[M] = ("---" + str (row+1) + "---" + div_coNTENT[M]). Encode (' utf-8 ') row + = 1 return div_content 


Next is mail.py. Attention. Sender's mailbox to check if the SMTP protocol is turned on

#!/usr/bin/env python#-*-coding:utf-8-*-import smtplibfrom email.mime.text Import Mimetextfrom email.header Import header__author__ = ' Sophie2805 ' class Smail:def __init__ (self): Self.sender = ' @qq. com ' self.receiver = [' ***@163.com ', ' * * * @qq. com '] Self.subject = ' embarrassed hundred popular 20 push ' Self.username = ' @qq. com ' self.password = ' ****** ' def send_mail (self,msg): try:self.msg = Mimetext (msg, ' pl Ain ', ' utf-8 ') self.msg[' Subject ' = Header (self.subject, ' utf-8 ') self.msg[' to '] = ', '. Join (self.rece Iver) self.msg[' from '] = self.sender self.smtp = smtplib. SMTP () self.smtp.connect (' smtp.qq.com ') self.smtp.login (Self.username,self.password) Self        . Smtp.sendmail (self.msg[' from '],self.receiver,self.msg.as_string ()) Self.smtp.quit () return ' 1 ' Except Exception, E:return str (e) 

Finally, using Wxpython to do a simple recipient management interface, spider_qiubai.py, realize: Add mail, delete mail, a key crawler send embarrassing hundred 20 to mailing list.

Of course, for the program to execute correctly, there will be a variety of verification, such as the format of the input mailbox is legitimate, sent to check if the receiver list is empty, etc.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvc29wagllmjgwnq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "width=" height= "630" >

The program is relatively simple, code such as the following:

#!/usr/bin/env python#-*-coding:utf-8-*-__author__ = ' Sophie2805 ' import wximport wx.gridfrom mail import *from Qiubai Import *class Frame (WX. Frame): Qiubai = None content = None M = Smail () def __init__ (self, parent=none, title= ' Get the hottest Qiubai ! ', size= (630), style=wx. Default_frame_style ^ (WX. Resize_border |wx. Maximize_box)): WX. Frame.__init__ (self, parent=parent, title=title, Size=size,style = style) self. Centre () panel = WX. Panel (self,-1) Self.text_email = wx. Statictext (panel,1, "You want it too? Add your email below. Using '; ' between multiple emails.        Existing emails won't be added again. ") Self.input_email = wx. Textctrl (panel,2) Self.button_email = wx. Button (panel,3,label= ' + ') ' Bind with function ' Self.button_Email.Bind (WX. Evt_button,self.add) Self.text_deleteemail = wx. Statictext (panel,4, "Existing receivers is listed below. You can delete by double clicking them and Clicking on '-' button. Please note the It can hold the emails at the most one time. ")"        Grid ' Self.grid = Wx.grid.Grid (panel,5) Self.grid.CreateGrid (20,1) self.grid.EnableEditing (False) Self.grid.SetColSize (0,200) self.grid.SetColLabelValue (0, ' Emails ') for I in Range (len (self.m.receiver) ): Self.grid.SetCellBackgroundColour (i,0, "Grey") Self.grid.SetCellValue (i,0,self.m.receiver[ I]) self.grid.Bind (wx.grid.evt_grid_cell_left_dclick,self.grid_change_color) Self.button_deleteemail = wx. Button (panel,6,label= '-') Self.button_send = wx. Button (panel,7,label= ' send ') ' Bind with function ' Self.button_Send.Bind (WX. Evt_button,self.sendmail) Self.button_DeleteEmail.Bind (WX. evt_button,self.d_emails) "Boxsizer" "================================================================ "' Add_email = wx. Boxsizer () #input box and add button Add_email.add (sElf.input_email,proportion=4,flag = wx. Left, border = 5) add_email.add (self.button_email,proportion=1,flag=wx. left,border=15) AE = wx. Boxsizer (WX. VERTICAL) Ae. ADD (Self.text_email,proportion =4, flag=wx. All, border=5) ae. ADD (add_email,proportion=2, flag=wx. Left|wx. Bottom|wx. Right, border=5) H_b = wx. Boxsizer () #send button and delete button H_b.add (self.button_send,flag=wx. left,border=5) H_b.add (self.button_deleteemail,flag=wx. left,border=250) de = wx. Boxsizer (WX. VERTICAL) de. ADD (self.text_deleteemail,proportion=2,flag=wx. All, border=5) de. ADD (self.grid,proportion = 4,flag=wx. Left, border=80) de. ADD (h_b,proportion=1,flag=wx. TOP, border=10) Vboxsizer = wx. Boxsizer (WX. VERTICAL) Vboxsizer.add (ae,proportion=2,flag=wx. all,border=5) Vboxsizer.add (de,proportion=4,flag=wx. Left|wx. Bottom|wx. right,border=5) panel.    Setsizer (Vboxsizer) #delete The emails that cell BK color changed to yellowdef d_emails (self,evt): Count_yellow = 0 for i in range (len (self.m.receiver)): If Self.grid.GetCe            Llbackgroundcolour (i,0) = = (255, 255, 0, 255): # yellow Count_yellow + = 1 if Count_yellow = = 0: Wx.        MessageBox (' Wake up!\nselect then delete! ') Else:l = Len (self.m.receiver) self.m.receiver=[] for I in range (l): if SE Lf.grid.GetCellBackgroundColour (i,0) = = (+,,, 255): #grey self.m.receiver.append (self.grid.GetCe Llvalue (i,0)) Self.grid.SetCellValue (i,0, ") #clear the cell ' s value Self.grid.SetCellBackgro Undcolour (i,0, ' white ') for I in Range (len (self.m.receiver)): Self.grid.SetCellBackgroundColour (i , 0, "Grey") Self.grid.SetCellValue (I,0,self.m.receiver[i]) Self.grid.ForceRefresh () #change CE ll BK color When double click the cell. The cell without value would not be changed DEF Grid_change_color (SELF,EVT): If Self.grid.GetCellValue (self.grid.gridcursorrow,self.grid.gridcursorcol)! = ": If Self.grid.GetCellBackgroundColour (self.grid.gridcursorrow,self.grid.gridcursorcol) = = (+,,, 255): #gre Y Self.grid.SetCellBackgroundColour (self.grid.gridcursorrow,self.grid.gridcursorcol, ' Yellow ') el Se:self.grid.SetCellBackgroundColour (Self.grid.gridcursorrow,self.grid.gridcursorcol, ' grey ') SELF.G Rid. Forcerefresh () #add The emails that user input def add (self,evt): x = str (Self.input_Email.GetValue (). Encode ( ' Utf-8 ')) If Len (x) = = 0:wx.        MessageBox (' Hey dude, input something first! ') Elif len (x)! = Len (X.decode (' Utf-8 ')): #non ASCII character contained wx.        MessageBox (' Hey dude, please input 中文版 character only! ')            else:x = X.split (';')             Tag = 0 for I in range (len (x)): If Len (X[i])! = 0:       If X[i] in self.m.receiver:x[i]= "#existing emails, set to" Elif self. Validate_email (x[i]) = = None:wx. MessageBox (' Hey dude, eyes wide open! \nplease input like this: [email protected];[ Email protected] ') tag = 1 break if tag = = 0: # all emails A Re valid for item in X:if len (item)! = 0:self.m.receiver.append (item) for I in Range (len (self.m.receiver)): Self.grid.SetCellBackgroundColour (i,0, "grey                ") Self.grid.SetCellValue (I,0,self.m.receiver[i]) self.input_Email.Clear () Self.grid.ForceRefresh () #validate The emails user input def validate_email (self,s): pattern = "^[a-za-z0-9 \._-][email protected] ([a-za-z0-9_-]+\.) + ([a-za-z]{2,3}) $ "P = re.compile (pattern) return P.match (s) dEF SendMail (SELF,EVT): If Len (self.m.receiver) = = 0:wx.        MessageBox (' kidding me?\n Add some receivers then send! ') Else:self. Qiubai = Fetchdata (' Http://www.qiushibaike.com/text ', ' http://www.qiushibaike.com/', ' <di V class= "content" >[\n\s]+.+[\n\s] ', ' [<div class= ' content ' ><br/>\n] ') self.content = self. Qiubai.getdata (self. Qiubai.gethtml ()) msg = ' \ n '. Join (self.content) result = Self.m.send_mail (msg) if Resul t = = ' 1 ': WX.            MessageBox (' The hottest Qiubai had been sent.\n Enjoy it ^_^! ') Else:wx. MessageBox (' Some exception occurred:-(\ntry again later...\n ' +result) class Getqiubaiapp (WX. APP): def OnInit (self): frame = FRAME () frame. Show () return trueif __name__ = = "__main__": App = Getqiubaiapp () app. Mainloop ()


Finally, the Python transfer app:

(1) Enter pip install Py2app installation Py2app in Mac Terminal

(2) Create setup.py, my project is located in /users/sophie/pycharmprojects/qiubai, so CD to this path. Then VI setup.py. Enter for example the following in this file (prepare a icns icon in advance.) Drop to project path)

"""

Py2app Build script for MyApplication


Usage:

Python setup.py Py2app

"""

From Setuptools Import Setup

OPTIONS = {

' IconFile ': ' Lemon.icns '

}

Setup

app=["spider_qiubai.py"],

options={' Py2app ': OPTIONS},

setup_requires=["Py2app"],

)

(3) RM-RF Build Dist (assuming that you pack it repeatedly.) This command is required. Not required for the first time)

(4) The python setup.py Py2app is packaged with this command, and the packaged app is stored under the Dist path under that path. My example is the following:/users/sophie/pycharmprojects/qiubai/dist

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvc29wagllmjgwnq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

At this point, this gadget has been done, a little bit of trouble may be the regular and interface Hope you enjoy it ^ ^

Python crawl 20 popular items and mail distribution +wxpython simple Gui+py2app to run files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.