python—到指定地址下载图片验证码,https,urllib2,urllib,time,os,ssl
1、Python源码
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import urllib
import urllib2
import string
import time
import os
import ssl
def downloadpic(numpic):#在特别指定URL地址去下载图片验证码,并保存为pic.png的图片
pwd = os.path.exists("e:/pic2")
if pwd:#判断文件夹是否存在,如果不存在则创建
print "File Exist!!!"
else:
os.mkdir("e:/pic2")
#下载图片验证码文件,并保存
for i in range(1,int(numpic)+1):
pic_url = "https://c.dinpay.com/checkNumber?temp=0.2549366261090778"#请求验证码生成页面的地址
context = ssl._create_unverified_context()#启用ssl。如果是http的话此行去除
pic_data_url = urllib2.urlopen(pic_url,context=context)#在urllib2启用ssl字段,打开请求的数据。如果是http的话此行去除字段context=context
pic_data = pic_data_url.read()#读取验证码图片
localtime = time.strftime("%Y%m%d%H%M%S",time.localtime())
filename = "e:/pic2/"+localtime+".png"#文件名格式
f = open(filename,"wb")
f.write(pic_data)
f.close()
print "file"+" "+str(i)+":"+str(localtime)+".png"
time.sleep(1)#暂停一秒
print "save ok!"
if __name__ == "__main__":
num = input("please input numbers of pic:")
downloadpic(num)
2、运行情况: