《Python实战:四周实现爬虫系统》百度网盘免费下载
链接: https://pan.baidu.com/s/1AwkMGdZ6z394WYE8yU0wRg
提取码: xbduPython实战:四周实现爬虫系统
# 以下程序可能要安装OpenCV2.0(并编译好并配置好环境)以及Xvid解码器才能运行# _*_coding: cp936_*_
import cv
capture = cv.CreateFileCapture("tmp.avi")
#请确保当前目录下有tmp.avi文件
fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)
totalFrameNumber =cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT)
frameWidth = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH)
frameHeight = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT)
print fps, totalFrameNumber, frameWidth, frameHeight
frame = cv.QueryFrame(capture)
while frame:
cv.ShowImage('Title', frame)
frame = cv.QueryFrame(capture)
if cv.WaitKey(1000 / fps) == 27:# ESC键退出视频播放
cv.DestroyWindow('Title')
break
一个文件夹整理工具wxPython版本(不清楚软件用法情况下可以打开该软件看看界面但请不要使用里面的功能,后果自负。)
# -*- coding: cp936 -*-
# file:aa.py
import os, shutil, sys
import wx
sourceDir = ''
sourceFiles = ''
preWord = 0
def browse(event):
global sourceDir, textPreWord, textDirName
dialog = wx.DirDialog(None, u'选择待处理文件夹', style = wx.OPEN)
if dialog.ShowModal() == wx.ID_OK:
sourceDir = dialog.GetPath().strip('\"')
textDirName.SetLabel(sourceDir)
dialog.Destroy()
textPreWord.SetFocus()
def okRun(event):
global preWord, sourceDir, sourceFiles, textPreWord
preWord = int(textPreWord.GetValue())
if preWord <= 0:
wx.MessageBox(u'请正确输入前缀字符个数!', u'出错啦', style = wx.ICON_ERROR | wx.OK)
textPreWord.SetFocus()
return
sourceDir = textDirName.GetValue()
sourceFiles = os.listdir(sourceDir)
for currentFile in sourceFiles:
tmp = currentFile
currentFile = sourceDir + '\\' + currentFile
currentFile = currentFile.strip('\"')
if os.path.isdir(currentFile):
continue
targetDir = '%s\\%s'%(sourceDir, tmp[:preWord])
if not os.path.exists(targetDir):
os.mkdir(targetDir)
shutil.move(currentFile, targetDir)
wx.MessageBox(u'任务完成!', u'好消息', style = wx.ICON_ERROR | wx.OK)
def onChange(event):
global dir1, textDirName
p = dir1.GetPath()
textDirName.SetLabel(p)
app = wx.App(0)
win = wx.Frame(None, title = u'文件整理', size = (400, 450))
bg = wx.Panel(win)
btnBrowse = wx.Button(bg, label = u'浏览')
btnBrowse.Bind(wx.EVT_BUTTON, browse)
btnRun = wx.Button(bg, label = u'整理')
btnRun.Bind(wx.EVT_BUTTON, okRun)
dir1 = wx.GenericDirCtrl(bg, -1, dir='', style=wx.DIRCTRL_DIR_ONLY)
tree = dir1.GetTreeCtrl()
dir1.Bind(wx.wx.EVT_TREE_SEL_CHANGED, onChange, id = tree.GetId())
textPreWord = wx.TextCtrl(bg)
textDirName = wx.TextCtrl(bg)
textPreWord.SetFocus()
class MyFileDropTarget(wx.FileDropTarget):#声明释放到的目标
def __init__(self, window):
wx.FileDropTarget.__init__(self)
self.window = window
def OnDropFiles(self, x, y, filenames):#释放文件处理函数数据
for name in filenames:
if not os.path.isdir(name):
wx.MessageBox(u'只能选择文件夹!', '出错啦', style = wx.ICON_ERROR | wx.OK)
return
self.window.SetValue(name)
dt = MyFileDropTarget(textDirName)
textDirName.SetDropTarget(dt)
hbox0 = wx.BoxSizer()
hbox0.Add(dir1, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)
hbox1 = wx.BoxSizer()
hbox1.Add(textDirName, proportion = 1, flag = wx.EXPAND, border = 5)
hbox1.Add(btnBrowse, proportion = 0, flag = wx.LEFT, border = 5)
hbox2 = wx.BoxSizer()
hbox2.Add(textPreWord, proportion = 1, flag = wx.EXPAND, border = 5)
hbox2.Add(btnRun, proportion = 0, flag = wx.LEFT, border = 5)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox0, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)
vbox.Add(wx.StaticText(bg, -1, u'\n 选择待处理文件夹路径:'))
vbox.Add(hbox1, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)
vbox.Add(wx.StaticText(bg, -1, u'\n\n 前缀字符个数:'))
vbox.Add(hbox2, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)
vbox.Add(wx.StaticText(bg, -1, u'\n'))
bg.SetSizer(vbox)
win.Center()
win.Show()
app.MainLoop()
#py2exe打包代码如下:
from distutils.core import setup
import py2exe
setup(windows=["aa.py"])
打包好了之后有18Mb。
一个文件内容批量替换摸查器(wxPython版本):
# _*_ coding:cp936 _*_
from string import join, split
import os
import wx
def getAllFiles(adir):
tmp = []
for parent, dirs, files in os.walk(adir):
for afile in files:
tmp.append(os.path.join(parent, afile))
return tmp
def browse(event):
dialog = wx.DirDialog(None, '选择待处理文件夹', style=wx.OPEN)
if dialog.ShowModal() == wx.ID_OK:
aDir = dialog.GetPath()
textDir.SetLabel(aDir)
dialog.Destroy()
def check():
promp.SetForegroundColour('#FF0000')
promp.SetLabel(' 搜索中...')
listFound.ClearAll()
tmp = []
files = getAllFiles(textDir.GetValue())
for filename in files:
try:
afile = open(filename, 'r')
except IOError:
print '打开文件错误。'
continue
try:
content = afile.read().decode('utf-8')
afile.close()
except:
try:
afile = open(filename, 'r')
content = afile.read().decode('cp936')
afile.close()
except:
continue
if textNeedChar.GetValue() in content:
tmp.append(filename)
listFound.InsertStringItem(0, filename)
parentDir.SetForegroundColour('#FF0000')
# parentDir.SetLabel(' 正在搜索:'+filename)
return tmp
def onFind(event):
check()
parentDir.SetLabel(' 搜索结果:')
promp.SetLabel(' 任务完成。')
wx.MessageBox(listFound.GetItemCount() and '任务完成,找到%d个文件。' % listFound.GetItemCount()
or '未找到包含"%s"的文件!' % textNeedChar.GetValue().encode('cp936'), '查找结束',
style=wx.ICON_INFORMATION | wx.OK)
def onReplace(event):
dg = wx.TextEntryDialog(bg, '替换成:', '提示', style=wx.OK | wx.CANCEL)
if dg.ShowModal() != wx.ID_OK:
return
files = check()
userWord = dg.GetValue()
#if not userWord:
#wx.MessageBox('请输入要替换为何字!', style=wx.ICON_ERROR | wx.OK)
#onReplace(event)
#return
for filename in files:
try:
afile = open(filename, 'r')
except IOError:
print '打开文件错误。'
continue
try:
content = afile.read().decode('utf-8')
afile.close()
except:
try:
afile = open(filename, 'r')
content = afile.read().decode('cp936')
afile.close()
except IOError:
print '打开文件错误。'
continue
content = content.replace(textNeedChar.GetValue(), userWord)
try:
content = content.encode('utf-8')
except:
pass
open(filename, 'w').write(content)
parentDir.SetLabel(' 替换的文件:')
promp.SetLabel(' 任务完成。')
wx.MessageBox(listFound.GetItemCount() and '任务完成,替换了%d个文件。' % listFound.GetItemCount()
or '未找到包含"%s"的文件!' % textNeedChar.GetValue().encode('cp936'), '替换结束', style=wx.ICON_INFORMATION | wx.OK)
app = wx.App(0)
win = wx.Frame(None, title='文件整理', size=(400, 450))
bg = wx.Panel(win)
btnBrowse = wx.Button(bg, label='浏览')
btnBrowse.Bind(wx.EVT_BUTTON, browse)
btnFind = wx.Button(bg, label='查找')
btnFind.Bind(wx.EVT_BUTTON, onFind)
btnFind.SetDefault()
btnReplace = wx.Button(bg, label='替换')
btnReplace.Bind(wx.EVT_BUTTON, onReplace)
textNeedChar = wx.TextCtrl(bg, value='你好')
textDir = wx.TextCtrl(bg, value='E:\Workspace\Python\Python\e')
parentDir = wx.StaticText(bg)
promp = wx.StaticText(bg)
listFound = wx.ListCtrl(bg, style=wx.VERTICAL)
class MyFileDropTarget(wx.FileDropTarget):#声明释放到的目标
def __init__(self, window):
wx.FileDropTarget.__init__(self)
self.window = window
def OnDropFiles(self, x, y, filenames):#释放文件处理函数数据
for name in filenames:
'''if not os.path.isdir(name):
wx.MessageBox('只能选择文件夹!', '出错啦', style=wx.ICON_ERROR | wx.OK)
return '''
self.window.SetValue(name)
dt = MyFileDropTarget(textDir)
textDir.SetDropTarget(dt)
hbox1 = wx.BoxSizer()
hbox1.Add(textDir, proportion=1, flag=wx.EXPAND, border=5)
hbox1.Add(btnBrowse, proportion=0, flag=wx.LEFT, border=5)
hbox2 = wx.BoxSizer()
hbox2.Add(textNeedChar, proportion=1, flag=wx.EXPAND, border=5)
hbox2.Add(btnFind, proportion=0, flag=wx.LEFT, border=5)
hbox2.Add(btnReplace, proportion=0, flag=wx.LEFT, border=5)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(wx.StaticText(bg, -1, '\n 选择待处理文件夹(可拖动文件夹到下面区域中):'))
vbox.Add(hbox1, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
vbox.Add(wx.StaticText(bg, -1, '\n\n 要替换 / 查找的文字:'))
vbox.Add(hbox2, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
vbox.Add(promp, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
vbox.Add(parentDir, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
vbox.Add(listFound, proportion=1,
flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)
bg.SetSizer(vbox)
win.Center()
win.Show()
app.MainLoop()
程序调用自身的编程技巧称为递归( recursion)。递归做为一种算法在程序设计语言中广泛应用。 一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法。
它通常把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解,递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算,大大地减少了程序的代码量。
递归的能力在于用有限的语句来定义对象的无限集合。一般来说,递归需要有边界条件、递归前进段和递归返回段。当边界条件不满足时,递归前进;当边界条件满足时,递归返回。
Python
是完全面向对象的语言。函数、模块、数字、字符串都是对象。并且完全支持继承、重载、派生、多继承,有益于增强源代码的复用性。Python支持重载运算符和动态类型。相对于Lisp这种传统的函数式编程语言,Python对函数式设计只提供了有限的支持。有两个标准库(functools, itertools)提供了Haskell和Standard ML中久经考验的函数式程序设计工具。