Python crawl Top 20 and mail distribution +wxpython simple Gui+py2app to executable file

Source: Internet
Author: User

Did you learn python for a while and do something interesting for me? Crawl embarrassing hundred good, crawl the content, the mail distributes out.

Then ate two days of Wxpython, did a Simple mail management interface, you can add or delete mail here, and one-click Crawler to send.

Finally, simply package into the app bar, and try a py2app, simple and easy to use.


The first is the qiubai.py: crawler, which matches what we want, and 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 up is mail.py, notice that the sender's mailbox checks to see if the SMTP protocol is open

#!/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, to achieve: add mail, delete mail, a key crawler to send embarrassing hundreds of popular 20 to the mailing list.

Of course, for the program to run 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.

The program is relatively simple, the code is as follows:

#!/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 the following in this file (prepare a icns icon in advance, put it under 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 (this command is needed for the first time if multiple packages are repeated)

(4) python setup.py py2app Use this command to package, the packaged app will be stored in the path under the Dist path, my following: /users/sophie/pycharmprojects/qiubai/dist


At this point, this gadget has been done, which may be slightly troublesome is the regular and interface Hope you enjoy it ^ ^

Python crawl Top 20 and mail distribution +wxpython simple Gui+py2app to executable file

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.