import requests,osfrom queue import Queuefrom hashlib import md5import base64import json,timefrom retrying import retryimport warnings# 取消warning提示warnings.filterwarnings("ignore")s = requests.session()s.verify = Falses.headers.update({'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36', })# q_cookis = Queue()def check_text(text): ''' 檢查是否充重試 :param text:網站源碼 :return: 不存在源碼中 ''' return text is not Truedef check_str(strs): ''' md5加密 :param 密碼 :return: md5加密後的32位 ''' m = md5() m.update(strs.encode(encoding='utf-8')) return m.hexdigest()@retry(retry_on_result=check_text, stop_max_attempt_number=3, wait_fixed=1000)def log_in(user,pasw): img_url = 'https://grwsyw.bjgjj.gov.cn/ish/ranCode.jsp?_='+str(int(time.time()*1000)) s.get('https://grwsyw.bjgjj.gov.cn/ish/') img = s.get(url=img_url) dic_ = json.loads(img.text) url = dic_['image'].replace('data:image/jpeg;base64,','') code_key = dic_['key'] f = open('code.jpg','wb') f.write(base64.b64decode(url)) f.close() os.system('code.jpg'.replace('/','\\')) codes = input('驗證碼:') data = { 'logintype': 'person', 'yzmkey': code_key, 'dlmm': check_str(pasw), 'zdyyhm': user, 'dlmmMD5': pasw, 'yzm': codes, 'xyjmdzd': '' } log_in_url = 'https://grwsyw.bjgjj.gov.cn/ish/login' s.post(url=log_in_url,data=data) myself_url = 'https://grwsyw.bjgjj.gov.cn/ish/home?_r=45362b79abc8e32c6652bc7ce446' check_text = s.get(myself_url) if '我的信息' in check_text.text: print('login Sucesses') # cookie = '; '.join(['='.join(item) for item in check_text.cookies.items()]) # q_cookis.put(cookie) return True else: return Falselog_in(user,pasw)