码迷,mamicode.com
首页 > 编程语言 > 详细

python基础

时间:2015-05-24 23:33:20      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

# -*- coding: utf-8 -*-

#作者:新手

__author__ = ‘Administrator‘

import urllib,urllib2,os,re

from PyQt4 import Qt,QtCore,QtGui

import  ConfigParser as Con

def Con1(com):

    print com

    #先等等(没什么想法)

def Message(path):

    message=QtGui.QMessageBox()

    message.setWindowTitle(u‘提示‘)

    message.setText(u‘真的要删除此文件夹吗?‘)

    okbutton=message.addButton(‘Yes‘,QtGui.QMessageBox.ActionRole)

    notbutton=message.addButton(‘No‘,QtGui.QMessageBox.ActionRole)

    message.exec_()

    button=message.clickedButton()

    if button==okbutton:

        import shutil

        shutil.rmtree(path)

    else:

        print u‘不删除此文件夹

#src="http://pic.meizitu.com/wp-content/uploads/2015a/05/39/limg.jpg"

#每次输入的目录不同,然后根据这个目录每次打开

def getCwd(cmd):

    if os.path.exists(cmd)==True:

        os.chdir(cmd)

        print u‘打开的目录地址是:‘,os.getcwd()

        print u‘是否需要打开:请根据下面序列进行操作;‘ \

              u‘1:打开‘ \

              u‘2:不打开

        num1=input(u‘请输入以上序列‘)

        if num1==‘1‘:

            os.startfile(cmd)

        else:

            if num1==‘2‘:

                print u‘不打开此目录

            try:

                print map(chr,[(str(num1))])

            except:print ‘error‘

 

 

def getHtl(path):

    ok1=0

    print path

    if os.path.exists(path)==False:

        os.mkdir(path)

        print u‘创建成功 路径名称叫{0}‘.format(path)

    else:

        pathOK=‘6‘

        print u‘文件目录已经存在,请删除或者重新创建新的

        while True:

            print u"""

                1:删除此文件夹

                2:创建新的目录

                3:删除之后创建新的目录(慎用)

                4:删除其他已创建的目录

                5:退出"""

            num=raw_input(u‘请选择序列:‘)

            if num==‘1‘:

                try:

                    import shutil

                    print u‘被删除的目录名称叫:‘,path

                    if os.path.exists(path)==True:

                       print ‘ok‘

                except WindowsError as Error:

                    print u‘错误‘,Error,u‘目录不存在

 

            elif num==‘2‘:

                try:

                    num1=raw_input(u‘请输入新目录:‘)

                    os.mkdir(num1)

                    print u‘新目录名称叫:‘,num1

                except WindowsError as error:

                    print error ,u‘文件夹重名

                    print u‘目录已经修改成{0}‘.format(num1)

                    print u‘是否修改到当前目录

                    if num1:

                        os.chdir(num1)

                        print u‘已经修改到{0}目录‘.format(num1)

                        print os.getcwd()

                        print u‘是否下次打开以最新的目录为准

                        if os.path.exists(num1)==True:

                            import sys

 

                            os.startfile(num1)

            elif num==‘3‘:

                path3=raw_input(ur‘请输入已创建的并要删除的目录:‘)

                if os.path.exists(path3)==True:

                    import shutil

                    shutil.rmtree(path3)

                    print u‘目录删除成功

                elif os.path.exists(path3)==False:

                    print u‘目录不存在,请检查目录完整性

                else:

                    num1=raw_input(u‘请输入新目录:‘)

                    os.mkdir(num1)

                    if os.path.exists(num1)==True:

                        os.startfile(num1)

            elif num==‘5‘:

                import sys

                print u‘退出

                sys.exit(0)

                break

            elif num==‘4‘:

                num2=raw_input(u‘请输入已创建的目录:‘)

                os.rmdir(num2)

                if os.path.exists(num2)==False:

                    print u‘请继续选择序列进行操作

                    if num==pathOK:

                        shutil.rmtree(num2)

                        print u‘删除的目录是‘,num2

            else:

                print u‘目录未存在或者选择序列未在其中,您输入的是{0},输入错误次数为{1}次,请重新检查‘.format(num,ok1+1)

 

            ok1+=1

def getHtml1(url):

    html=urllib2.Request(url)

    html1=urllib2.urlopen(html).read()

    return html1

def getJpg(html,path=r‘G:\img‘):

    reg=r‘src="(.+?\.jpg)" alt=‘

    imgre=re.compile(reg)

    imglist=re.findall(imgre,html)

    x=0

    import datetime,time

    date=datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)

    date1=datetime.datetime.now().strftime(‘%S‘)

    print path

    if os.path.exists(path)==False:

        os.mkdir(path)

    for root,dirs,fies in os.walk(path):

        for j in fies:

            if j:

                print u‘相同‘,u‘无法下载‘#修改

                getHtl(path)

                break

 

        else:

            for url1 in imglist:

                p1=url1.split(‘.‘)[2].split(‘/‘)[3]+url1.split(‘.‘)[2].split(‘/‘)[4]+url1.split(‘.‘)[2].split(‘/‘)[5]

                urllib.urlretrieve(url1,r‘G:\img‘+‘\\‘+(r‘%s.jpg‘%(x)))

                print u‘正在下载第{0},名称叫{1}‘.format(x+1,r‘%s.jpg‘%(x))

                x+=1

            print u‘下载完成,开始下载时间是{0},一共需要下载{1}:‘.format(date,date1)

            time.sleep(1)

url1=‘http://www.meizitu.com/a/list_1_1.html‘

path1=getHtml1(url1)

getJpg(path1)

 

 

 技术分享

 

python基础

标签:

原文地址:http://www.cnblogs.com/mhxy13867806343/p/4526670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!