Python3.4---实战项目-自动下载图片验证码,并保存到专门文件夹,使用图片验证码库识别,然后打印出来

Python3.4—实战项目-自动下载图片验证码,并保存到专门文件夹,使用图片验证码库识别,然后打印出来

参考:https://blog.csdn.net/xwbk12/article/details/78824813
https://blog.csdn.net/vivian_ll/article/details/75615703
https://zhidao.baidu.com/question/139441809367947885.html?qbl=relate_question_0&word=python3.4%20urllib2
https://segmentfault.com/q/1010000007702406

1、环境部署
参考文章《使用Python识别图片验证码》内容,制造好图片验证码识别库msweb1.dat
1.1、对图片验证码随时下载的URL地址:http://www.msweb.com:8080/checkNumber.action

1.2、代码文件保存文件夹:C:\Python34\yzm\msweb1\mswebyzm,以及图片验证码被保存到的文件夹:C:\Python34\

1.3、图片验证码URL下载保存的文件夹:C:\Python34\yzm\msweb1\mswebyzm

2、python脚本的源码文件

#coding=gbk
#必须使用gbk语言格式

import os
import urllib
import urllib.request#在python3.x中没有urllib2,必须使用此语句
import time
import string
import ctypes
from ctypes import * 

def identifypicyzm(picyzm):#使用图片验证码识别库识别图片验证码
        dll = ctypes.windll.LoadLibrary('C:/Python34/WmCode.dll')
        if(dll.UseUnicodeString(1,1)): 
                print('SetInUnicode Success:')
        else:
                print('etInUnicode Fail!')
        if(dll.LoadWmFromFile('C:/Python34/msweb1.dat','123456')):#使用图片验证码识别库,123456为识别库的密码
                print('Loaddat Success:')
                Str = create_string_buffer(20)
                filepath = "C:/Python34/yzm/msweb1/mswebyzm/"+picyzm#组装图片验证码的图片绝对路径
                print (filepath)
                if(dll.GetImageFromFile(filepath,Str)):
                        print('GetVcode Success:',Str.raw.decode("gbk"))
                        print ('\n')
                else:
                        print('GetVcode Fail!')         
        else:
                print('Loaddat Fail!')

def downloadpic(numpic):#批量下载图片验证码文件
        pwd = os.path.exists('C:/Python34/yzm/msweb1/mswebyzm/')
        if pwd:#判断文件夹是否存在
                print ('file exist!')
        else:
                os.mkdir('C:/Python34/yzm/msweb1/mswebyzm/')#不存在自动新建文件夹mswebyzm
        numpic = int(numpic)
        for i in range(1,numpic+1):
                pic_url = "http://www.msweb.com:8080/checkNumber.action"#图片验证码获取的URL
                pic_data_url = urllib.request.urlopen(pic_url)#python3.4的urllib语句
                pic_data = pic_data_url.read()
                localtime = time.strftime("%Y%m%d%H%M%S",time.localtime())
                filename = "C:/Python34/yzm/msweb1/mswebyzm/"+localtime+".jpg"#创建文件
                f = open(filename,"wb")
                f.write(pic_data)
                f.close()
                print ("file"+ "  "+str(i)+":"+str(localtime)+".jpg")
                time.sleep(1)
        print ("finsh!!")

def picyzmlist():#把图片验证码目录下所有文件统计出来,并保存到列表中
    count = 0
    for filename in os.listdir('C:/Python34/yzm/msweb1/mswebyzm/'):#读取文件目录
        #print (filename)#输入每个文件的名称
        yzmlist.append(filename)
        count += 1#文件数量自加1
    #print (count)#文件的数量
    #print (filelist)

if __name__ == "__main__":
    yzmlist = []#用来统计被下载的图片验证码所有图片的文件名称的列表
    num = input("please input pic num:")
    downloadpic(num)
    picyzmlist()
    #print (yzmlist)
    print (len(yzmlist))#len(yzmlist)计算列表的长度
    for i in range(0,len(yzmlist)-1):
        print('The No. is:',i+1)
        identifypicyzm(yzmlist[i])
        time.sleep(0.5)

3、脚本运行情况

C:\Python34>python C:\Python34\yzm\msweb1\mswebyzm2.py
please input pic num:1
file exist!
file  1:20180509162107.jpg
finsh!!
10
The No. is: 1
SetInUnicode Success:
Loaddat Success:
C:/Python34/yzm/msweb1/mswebyzm/20180509152051.jpg
GetVcode Success: C4AA

The No. is: 2
SetInUnicode Success:
Loaddat Success:
C:/Python34/yzm/msweb1/mswebyzm/20180509152230.jpg
GetVcode Success: STQ

The No. is: 3
SetInUnicode Success:
Loaddat Success:
C:/Python34/yzm/msweb1/mswebyzm/20180509152419.jpg
GetVcode Success: 5D

The No. is: 4
SetInUnicode Success:
Loaddat Success:
C:/Python34/yzm/msweb1/mswebyzm/20180509152420.jpg
GetVcode Success: 5NY

The No. is: 5
SetInUnicode Success:
Loaddat Success:
C:/Python34/yzm/msweb1/mswebyzm/20180509152421.jpg
GetVcode Success: B7P3

The No. is: 6
SetInUnicode Success:
Loaddat Success:
C:/Python34/yzm/msweb1/mswebyzm/20180509153309.jpg
GetVcode Success: CCT

The No. is: 7
SetInUnicode Success:
Loaddat Success:
C:/Python34/yzm/msweb1/mswebyzm/20180509153417.jpg
GetVcode Success: 3A8

The No. is: 8
SetInUnicode Success:
Loaddat Success:
C:/Python34/yzm/msweb1/mswebyzm/20180509153907.jpg
GetVcode Success: 8H6G

The No. is: 9
SetInUnicode Success:
Loaddat Success:
C:/Python34/yzm/msweb1/mswebyzm/20180509153908.jpg
GetVcode Success: 96G


C:\Python34>

你可能感兴趣的:(个人所悟,python,os-system,图片)