Spaces:
Running
Running
# -*- coding: utf-8 -*- | |
import json | |
import requests | |
import time | |
from twocaptcha import TwoCaptcha | |
from cairosvg import svg2png | |
# two cap tcha API密钥 | |
api_key = '50c4d9078bc12ca8c101f0900ec0c80f' | |
captcha_image_save = 'captcha.png' | |
# 定义请求头信息 | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36' | |
} | |
def current_milli_time(): | |
return round(time.time() * 1000) | |
session = requests.Session() | |
session.verify =False | |
session.headers = headers | |
# #传入账号密码获取返回结果的响应头从而得到cookie和截止时间 | |
def get_response_headers(account:str,password:str,captcha_text:str): | |
login_url = 'https://jingling.bifangpu.com/user/v1/login' | |
payload = { | |
"email": account, | |
"password": password, | |
"captcha": captcha_text | |
} | |
response = session.post(login_url, data=payload) | |
# 获取响应头 | |
headers = response.headers | |
print(headers) | |
return headers | |
def get_cur_captcha(timeStamp:int,solver)->'返回当前时间戳对应的验证码数字结果 或者 None': | |
geetest_base ='https://jingling.bifangpu.com/user/v1/captcha?t=' | |
geetest_image_url = geetest_base+str(timeStamp)#这个只要请求了那么最新的验证码就是这个,并且自己写时间戳不影响截止时间 | |
img_resp= session.get(geetest_image_url) | |
if img_resp.status_code==200: | |
img_content = img_resp.content | |
svg2png(img_content, write_to=captcha_image_save) | |
id = solver.send(file=captcha_image_save) | |
finalText = None | |
for i in range(5): | |
try: | |
time.sleep(10) | |
finalText = solver.get_result(id) | |
break | |
except Exception as e: | |
print(e) | |
if finalText and type(finalText) == str: | |
print("验证码处理成功") | |
print(finalText) | |
return finalText | |
else: | |
print('保存captcha图片失败') | |
return None | |
# def cookies_from_json_to_cookiejar(cookie:dict): | |
# # {'jinglingpan:sessid': '608c4163-16fd-4326-8caf-7b4076fdcaca', | |
# # 'jinglingpan:sessid.sig': 'qEDpK0oDIYLJXZbO1P7UjmJ0OjY'} | |
# return requests.utils.cookiejar_from_dict(cookie) | |
# | |
# def cookies_from_cookiejar_to_json(cookie)->dict: | |
# return requests.utils.dict_from_cookiejar(session.cookies) | |
def get_cookie_alive_str(): | |
account, password = "3357172372@qq.com", "17331160967a" | |
current_timestamp = current_milli_time() | |
solver = TwoCaptcha(api_key) | |
result = get_cur_captcha(current_timestamp, solver) | |
if result: | |
resp_headers = get_response_headers(account, password, result) | |
set_cookies = resp_headers['Set-Cookie'] | |
print('#截止时间信息:', set_cookies) | |
# 单引号就可以 | |
print('cookie=' + json.dumps(requests.utils.dict_from_cookiejar(session.cookies))) | |
return '#截止时间信息:'+ set_cookies+'\n'+'cookie=' + json.dumps(requests.utils.dict_from_cookiejar(session.cookies)) | |
else: | |
return '' | |
if __name__ == '__main__': | |
account,password = "3357172372@qq.com","17331160967a" | |
current_timestamp = current_milli_time() | |
solver = TwoCaptcha(api_key) | |
result = get_cur_captcha(current_timestamp,solver) | |
if result: | |
resp_headers = get_response_headers(account,password,result) | |
set_cookies = resp_headers['Set-Cookie'] | |
print('#截止时间信息:',set_cookies) | |
#单引号就可以 | |
print('cookie='+json.dumps(requests.utils.dict_from_cookiejar(session.cookies))) | |