def main(): for i in range(200): ik = get_random_id() u = image_api.format(id=ik) r = requests.get(u, headers=img_api_headers) url, filename = get_image_url_and_filename(r.text) save_url_image_to_file(url, filename)
def send_query_until_true(num): # 生成准考证号 new_id = myid.format(id=num) # 获取验证码图片地址 img_api_url = image_api.format(id=new_id) img_api_resp = requests.get(img_api_url, headers=img_api_headers) img_url, filename = get_image_url_and_filename(img_api_resp.text) print() # 获取验证码图片并猜测 img_resp = requests.get(img_url) code = get_validate_code_from_image(Image.open(BytesIO(img_resp.content))) # 执行查询操作 data = { "data": "CET4_181_DANGCI,{id},{name}".format( id=new_id, name=name), #看您是四级还是六级考试的修改为CET4_181还是CET6_181 "v": code } log_info(data) query_resp = requests.post(query_api, data=data, headers=query_api_headers) query_text = query_resp.text log_info(query_text) if "验证码错误" in query_text: query_text = send_query_until_true(num) return query_text
def save_image_to_file(): myid = "340080181210211" new_id = myid.format(id=myid) img_api_url = image_api.format(id=new_id) img_api_resp = requests.get(img_api_url, headers=img_api_headers, timeout=10) img_url, filename = get_image_url_and_filename(img_api_resp.text) r = requests.get(img_url) with open("images/raw_picture/" + filename, "wb+") as f: f.write(r.content) '''
def send_query_until_true(num): # 生成准考证号 global proxy new_id = myid.format(id=num) # 获取验证码图片地址 img_api_url = image_api.format(id=mid) # img_api_url = 'http://cet.neea.edu.cn/imgs/28e6e9985b6e4d4880db307edfd66413.png' while True: try: img_api_resp = requests.get(img_api_url, headers=img_api_headers, timeout=10) img_url, filename = get_image_url_and_filename(img_api_resp.text) print(img_url) # 获取验证码图片并猜测 img_resp = requests.get(img_url, timeout=10) if img_resp.status_code == 200: images = Image.open(BytesIO(img_resp.content)) code = img_verify_code(images) else: code = "xxxx" except Exception: print("重新获取代理") p = str(get_proxy()) proxy = {'http': 'http://' + p, 'https': 'http://' + p} else: break # CET4成绩查询选项 # data = {"data": "CET4_181_DANGCI,{id},{name}".format(id=new_id, name=name),"v": code} # CET6成绩查询选项 data = { "data": "CET6_181_DANGCI,{id},{name}".format(id=new_id, name=name), "v": code } query_resp = requests.post(query_api, data=data, headers=query_api_headers) query_text = query_resp.text log_info(query_text.split("'")[3], new_id) if "验证码错误" in query_text: query_text = send_query_until_true(num) # elif "您查询的结果为空" in query_text: # images.save("images/save_picture/" + code + ".png") return query_text
def send_query_until_true(myid, name, type): # 生成准考证号 new_id = myid # 获取验证码图片地址 img_api_url = image_api.format(id=new_id) img_api_resp = requests.get(img_api_url, headers=img_api_headers) img_url, filename = get_image_url_and_filename(img_api_resp.text) # 获取验证码图片并猜测 img_resp = requests.get(img_url) code = get_validate_code_from_image(Image.open(BytesIO(img_resp.content))) # 执行查询操作 data = {} if type == 4: data = { "data": "CET4_172_DANGCI,{id},{name}".format(id=new_id, name=name), "v": code } log_info(data) query_resp = requests.post(query_api, data=data, headers=query_api_headers) query_text = query_resp.text log_info(query_text) # if "验证码错误" in query_text: # query_text = send_query_until_true(num, type) if "验证码错误" in query_text: return False return query_text else: data = { "data": "CET6_172_DANGCI,{id},{name}".format(id=new_id, name=name), "v": code } log_info(data) query_resp = requests.post(query_api, data=data, headers=query_api_headers) query_text = query_resp.text log_info(query_text) if "验证码错误" in query_text: return False return query_text
def send_query_until_true(js, zw): global cnt # 生成准考证号 new_id = myid.format(js=js, zw=zw) # 获取验证码图片地址 img_api_url = image_api.format(id=new_id) img_api_resp = requests.get(img_api_url, headers=img_api_headers) img_url, filename = get_image_url_and_filename(img_api_resp.text) # 获取验证码图片并猜测 img_resp = requests.get(img_url, stream=True) if img_resp.status_code == 200: code = get_validate_code_from_image(Image.open(BytesIO(img_resp.content))) else: # 404后直接赋值一个错误的code, 以便接下来递归回来 code = "xxxx" # 执行查询操作 cnt += 1 data = { "data": "CET4_181_DANGCI,{id},{name}".format(id=new_id, name=name), "v": code } #log_info(data) print("Try: " + data['data'].split(',')[1] + "," +data['data'].split(',')[2]) # threadLock.acquire() try: query_resp = requests.post(query_api, data=data, headers=query_api_headers, timeout=5) if query_resp.status_code == 200: query_text = query_resp.text except requests.exceptions.Timeout: global NETWORK_STATUS NETWORK_STATUS = False if NETWORK_STATUS == False: for i in range(1, 10): print("第%s次超时,正在重新请求."%i) query_resp = requests.post(query_api, data=data, headers=query_api_headers, timeout=5) if query_resp.status_code == 200: query_text = query_resp.text break # threadLock.release() print("Result: " + query_text) if "验证码错误" in query_text: query_text = send_query_until_true(js, zw) ''' if not os.path.exists("download_error_capture"): os.mkdir('download_error_capture') with open("download_error_capture/%s.png" % random.randint(10000, 99999), "wb") as f: f.write(img_resp.content) f.close() ''' else: if not os.path.exists("download_capture"): os.mkdir('download_capture') with open("download_capture/%s.png" %code, "wb") as f: f.write(img_resp.content) f.close() return query_text