刚开始在 except Exception as e: 中直接写的get_token(),
结果第一次执行,无值取[0],报错进入异常,
然后多次之后,有值,跳出函数 return oohTokenName, oohTokenValue
oohTokenName, oohTokenValue = get_token() 却为空,
需要在except Exception as e: 修改为 return get_token()
def get_token():
url = 'https://www.xxxxx.com/user/login'
html = session.get(url, headers=headers).text
print(html)
try:
print('>>>>>>>>>', re.findall('ooh.token.name" value="(.*?)"', html))
print('>>>>>>>>>', re.findall('ooh.token.value" value="(.*?)"', html))
oohTokenName = re.findall('ooh.token.name" value="(.*?)"', html)[0]
oohTokenValue = re.findall('ooh.token.value" value="(.*?)"', html)[0]
return oohTokenName, oohTokenValue
except Exception as e:
print(f"An error occurred: {e}, retrying in 3 seconds...")
time.sleep(3)
# 这里需要 return get_token()
# 不能直接写成 get_token()
return get_token()
oohTokenName, oohTokenValue = get_token()