写使用python PAMIE模块写一个自动操作IE的脚本,但是获取验证码图片时遇到困难,试过使用win32inet.GetUrlCacheEntryInfo
从IE缓存读文件,但读普通图片没问题,一读验证码图片就报错:
pywintypes.error: (2, 'GetUrlCacheEntryInfo', '\xcf\xb5\xcd\xb3\xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xce\xc4\xbc\xfe\xa1\xa3')
求获取验证码相关解决方案:
import cPAMIE
import urllib
import urllib2
def deCaptcha(function, image_source=None):
"""调用 de-captcher API 解析验证码图片
Args:
function: str
balance: 查询余额,
picture2: 解析图片
image_source: str,图片源文件
Returns:
dict,de-captcher 解析返回信息
{
'ResultCode': 0,
'MajorID': 107,
'MinorID': 44685,
'Type': 0,
'Timeout': 0,
'Text': 'n7hjks'
}
"""
data = {
'username': 'username',
'password': 'password',
'function': function,
'pict_to': '0',
'pict_type': '0',
'pict': image_source
}
keys = 'ResultCode|MajorID|MinorID|Type|Timeout|Text'.split('|')
de_captcher_server = "http://poster.de-captcher.com/"
if function is 'balance' and image_source is None:
data.pop('pict')
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
result = opener.open(de_captcher_server, urllib.urlencode(data))
return result
elif function is 'picture2' and image_source:
try:
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
result = opener.open(de_captcher_server, urllib.urlencode(data))
return dict(zip(keys, result.read().split('|')))
except KeyError:
return result
def getCaptchaImage(url):
"""获取验证码图片
Args:
url: 字符串,图片的网址
Returns:
字符串,图片源文件
fileObj.read()
"""
pass
# 求获取验证码图片的方法
browser = cPAMIE.PAMIE()
browser.navigate('http://example.com/')
captcha_image_src = browser.imageGet('image_element_ID').src
image_source = getCaptchaImage(captcha_image_src)
print deCaptcha(image_source)['Text']