一,去云打码注册登录管理自己的服务;调用里面的官网函数获取我们想要的验证码,具体操作看文档。
def discern(filepath,codetype):
# 用户名
username = '1130402601'
# 密码
password = '2716532cxw'
# 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!
appid = 6124
# 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!
appkey = '2e20bec419802d64b56a89f8e42fe985'
# 超时时间,秒
timeout = 60
# 初始化
yundama = YDMHttp(username, password, appid, appkey)
# 登陆云打码
yundama.login();
# 查询余额
balance = yundama.balance();
print('balance: %s' % balance)
#开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
cid, result = yundama.decode(filepath, codetype, timeout);
return result
二,使用selenium和PhantomJS
安装教程
wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
tar -xvjf phantomjs-2.1.1-linux-x86_64.tar.bz2
sudo cp -R phantomjs-2.1.1-linux-x86_64 /usr/local/share/
sudo ln -sf /usr/local/share/phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/
三,全部代码
1,text_使用selenium访问豆瓣网验证码处理.py(核心逻辑)
import time
import requests
from selenium import webdriver
from yundama import discern
# driver = webdriver.PhantomJS()
driver = webdriver.Chrome("/home/cuixin/chromedriver")
driver.get("http://www.douban.com")
# 输入账号密码
driver.find_element_by_name("form_email").send_keys("3118870566@qq.com")
driver.find_element_by_name("form_password").send_keys("hello123")
#获取图片
valicode_url = driver.find_element_by_id("captcha_image").get_attribute("src")
resp = requests.get(valicode_url)
file_path = "./files/captcha_image.jpeg"
with open(file_path, "wb") as file:
file.write(resp.content)
#调用云打码
valicode = discern(filepath=file_path,codetype=3000)
print(valicode)
#输入验证码
driver.find_element_by_name("captcha-solution").send_keys(valicode)
# 等待
time.sleep(2)
# 模拟点击登录
driver.find_element_by_xpath("//input[@class='bn-submit']").click()
# 等待
print("登录中......")
time.sleep(10)
print(driver.title)
# 生成登陆后快照
# driver.save_screenshot("./files/douban.png")
# 保存源码
with open("./files/douban_new.html", "w") as file:
file.write(driver.page_source)
#
# #退出
driver.quit()
2,yundama.py(代码来自云API文档,只需要改下discern函数里面的参数,要求规范填写)
import http.client, mimetypes, urllib, json, time, requests
class YDMHttp:
apiurl = 'http://api.yundama.com/api.php'
username = ''
password = ''
appid = ''
appkey = ''
def __init__(self, username, password, appid, appkey):
self.username = username
self.password = password
self.appid = str(appid)
self.appkey = appkey
def request(self, fields, files=[]):
response = self.post_url(self.apiurl, fields, files)
response = json.loads(response)
return response
def balance(self):
data = {'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid,
'appkey': self.appkey}
response = self.request(data)
if (response):
if (response['ret'] and response['ret'] < 0):
return response['ret']
else:
return response['balance']
else:
return -9001
def login(self):
data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid,
'appkey': self.appkey}
response = self.request(data)
if (response):
if (response['ret'] and response['ret'] < 0):
return response['ret']
else:
return response['uid']
else:
return -9001
def upload(self, filename, codetype, timeout):
data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid,
'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)}
file = {'file': filename}
response = self.request(data, file)
if (response):
if (response['ret'] and response['ret'] < 0):
return response['ret']
else:
return response['cid']
else:
return -9001
def result(self, cid):
data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid,
'appkey': self.appkey, 'cid': str(cid)}
response = self.request(data)
return response and response['text'] or ''
def decode(self, filename, codetype, timeout):
cid = self.upload(filename, codetype, timeout)
if (cid > 0):
for i in range(0, timeout):
result = self.result(cid)
if (result != ''):
return cid, result
else:
time.sleep(1)
return -3003, ''
else:
return cid, ''
def report(self, cid):
data = {'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid,
'appkey': self.appkey, 'cid': str(cid), 'flag': '0'}
response = self.request(data)
if (response):
return response['ret']
else:
return -9001
def post_url(self, url, fields, files=[]):
for key in files:
files[key] = open(files[key], 'rb');
res = requests.post(url, files=files, data=fields)
return res.text
######################################################################
def discern(filepath,codetype):
# 用户名
username = '1130402601'
# 密码
password = '2716532cxw'
# 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!
appid = 6124
# 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!
appkey = '2e20bec419802d64b56a89f8e42fe985'
# 超时时间,秒
timeout = 60
# 初始化
yundama = YDMHttp(username, password, appid, appkey)
# 登陆云打码
yundama.login();
# 查询余额
balance = yundama.balance();
print('balance: %s' % balance)
#开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
cid, result = yundama.decode(filepath, codetype, timeout);
return result
if __name__ == '__main__':
discern("./11.jpg",5000)