一、代码
二、出错
可是下载了一百多篇论文就报错了,原来出现了验证码。可是我设定为自动识别验证码了,还是报以下的错误。
ERROR:root:出现验证码
Traceback (most recent call last):
File "main.py", line 144, in parse_page
tr_table.tr.extract()
AttributeError: 'NoneType' object has no attribute 'tr'
原来采集了10多页的时候,会跳出验证码的页面:
通过查看源码,发现验证码图片的地址为:
https://kns.cnki.net/kns/checkcode.aspx?t=%270.0684436452893744
需要构建参数:
rurl:/kns/brief/brief.aspx?curpage=17&RecordsPerPage=20&QueryID=1&ID=&turnpage=1&tpagemode=L&dbPrefix=SCDB&Fields=&DisplayMode=listmode&PageName=ASP.brief_result_aspx&isinEn=1& vericode:"RLVB4"
三、云打码
http://www.yundama.com
使用非常方便
不过注册默认是没有积分的,我开始注册了普通用户,可是无法取得appkey,appid,后来又注册了一个开发者账户,发现开发者账户又无法充值,我联系在线客服QQ,无人应答,然后打网站上留的手机号码,结果关机。反正这个网站设计得有点脑残。后来我在普通用户那里充值,然后使用开发者账户的appkey,appid,终于识别成功了。
充值了一元,得到2000分,识别一个码要花费10分。
代码
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 ###################################################################### # 用户名 username = 'd8' # 密码 password = 'k4' # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得! appid = 10405 # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得! appkey = '643aa' # 图片文件 filename = '33.gif' # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html codetype = 1005 # 超时时间,秒 timeout = 60 # 检查 if (username == 'username'): print('请设置好相关参数再测试') else: # 初始化 yundama = YDMHttp(username, password, appid, appkey) # 登陆云打码 uid = yundama.login(); print('uid: %s' % uid) # 查询余额 balance = yundama.balance(); print('balance: %s' % balance) # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果 cid, result = yundama.decode(filename, codetype, timeout); print('cid: %s, result: %s' % (cid, result)) ######################################################################
参考:https://www.bilibili.com/video/BV1Vb411F7yG?from=search&seid=292596025331943924