退出python函数错误
我有一个在某个网站上创建帐户的功能。它使它自动解决验证码 以下是我的功能:退出python函数错误,python,Python,我有一个在某个网站上创建帐户的功能。它使它自动解决验证码 以下是我的功能: def create_account(): global login global password global email print('# REJESTROWANIE NOWEGO KONTA') s=requests.Session() headers = { 'Accept-Encoding': 'gzip, deflate, sdch',
def create_account():
global login
global password
global email
print('# REJESTROWANIE NOWEGO KONTA')
s=requests.Session()
headers = {
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'en-US,en;q=0.8',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
}
s.headers.update(headers)
r=s.get(REGISTER_URL)
soup=BeautifulSoup(r.content, "html.parser")
captcha_img=soup.find("img",{"alt":"captcha"})['src']
nazwa = basename(captcha_img)
capublic = str(os.path.splitext(nazwa)[0])
with open('/root/environments/captcha_img/'+nazwa,"wb") as f:
f.write(requests.get('https://www.mywebsite.net/'+captcha_img).content)
captcha_text = ''
try:
captcha_text = captcha('captcha_img/'+nazwa)
filelist = [ f for f in os.listdir('/root/environments/captcha_img/') if f.endswith(".png") ]
for f in filelist:
os.remove(os.path.join('/root/environments/captcha_img/', f))
except:
print('> nieznany typ captcha')
print('> ponawiam')
text = ''
create_account()
data={
"jscheck": '1',
"login": login,
"pass": password,
"pass2":password,
"email": email,
"cacheck":captcha_text,
"capublic":capublic,
'button_submit': 'Sign+Up+(Free)',
}
r=s.post(REGISTER_URL,headers=headers,data=data)
text = str(r.content)
koniec = 'nie'
if "Your registration was successful" in text:
print('# ZAREJESTROWANO')
print('> login: '+login+', hasło: '+password+', email: '+email)
text = ''
koniec = 'tak'
return
elif "The supplied Captcha is wrong." in text and koniec != 'tak':
print('# BŁĘDNE CAPTCHA')
print('> ponawiam')
text = ''
create_account()
elif "Please fill out both Passwordfields." in text and koniec != 'tak':
print('# NIE WPISANO HASŁA')
print('> ponawiam')
text = ''
create_account()
elif "The supplied Passwords do not match." in text and koniec != 'tak':
print('# HASŁA NIE SĄ TAKIE SAME')
print('> ponawiam')
text = ''
create_account()
elif "Please enter your Username." in text and koniec != 'tak':
print('# NIE WPISANO LOGINU')
print('> ponawiam')
text = ''
create_account()
elif "The Username is already in use." in text and koniec != 'tak':
print('# LOGIN ZAJĘTY')
print('> generuję nowy login')
login = get_uname(5, 10, False)
print('> ponawiam')
text = ''
create_account()
elif "The supplied E-Mail is already in use." in text and koniec != 'tak':
print('# ADRES EMAIL ZAJĘTY')
print('> pobieram nowy adres e-mail')
email = get_email()
print('> ponawiam')
text = ''
create_account()
else:
print('# INNY NIEZNANY BŁĄD')
print('> generuję nowe dane logowania')
login = get_uname(5, 10, False)
password = password = get_password(8)
email = get_email()
print('> ponawiam')
text = ''
create_account()
有时在解决验证码时会出现错误。有两个错误:验证码错误或验证码类型未知。在这两种情况下,我都再次运行我的函数
当这两个错误中的一个出现时,就会出现错误,如本例所示:
如您所见,首先出现一个错误无效验证码类型(nieznany类型验证码),然后函数再次运行并成功创建帐户(ZAREJESTROWANO),函数应该停止。在我的代码中:
if "Your registration was successful" in text:
print('# ZAREJESTROWANO')
print('> login: '+login+', hasło: '+password+', email: '+email)
text = ''
koniec = 'tak'
return
但正如您在图像(从控制台)上看到的,它再次运行(BŁĘDNE CAPTCHA)
当没有错误验证码且成功创建帐户时,一切正常,功能停止
我尝试过清除“text”变量,甚至添加“koniec”变量,但这并不能解决问题。有什么想法吗?如果1)不希望函数完全重置,2)不希望求值返回到调用函数的点(例如,在except块中),则不应再次调用
create\u account()
另外,koniec='nie'
将使其始终设置在if语句之前,因此将其与任何其他内容进行检查是没有意义的
相反,更改代码以遵循此模式
def create_account():
registered = False
while not registered:
# do work
try:
captcha_text = captcha('captcha_img/'+nazwa)
except:
continue # repeat the loop
if "Your registration was successful" in text:
registered = True
return
elif "The supplied Captcha is wrong." in text:
# just let the while loop repeat the function on its own
continue # or call continue
如果1)不希望函数完全重置,2)不希望求值返回到调用函数的点(例如,在except块中),则不应再次调用
create_account()
另外,koniec='nie'
将使其始终设置在if语句之前,因此将其与任何其他内容进行检查是没有意义的
相反,更改代码以遵循此模式
def create_account():
registered = False
while not registered:
# do work
try:
captcha_text = captcha('captcha_img/'+nazwa)
except:
continue # repeat the loop
if "Your registration was successful" in text:
registered = True
return
elif "The supplied Captcha is wrong." in text:
# just let the while loop repeat the function on its own
continue # or call continue
在try/except块中调用
create\u account()
似乎是在代码执行到一半时出现的问题,它将递归,返回到周围的代码,然后再继续执行if/else块。(解释输出的事件顺序)
顺便说一句,这段代码的整体结构相当混乱,我怀疑这是因为您没有使用for循环或while循环来代替递归。另外,您应该尽可能避免使用
全局
变量,因为它们也会混淆程序的控制流,尤其是与递归混淆。看起来,在try/except块中调用create_account()
是问题所在,它会递归,返回到周围的代码,然后将继续执行if/else块。(解释输出的事件顺序)
顺便说一句,这段代码的整体结构相当混乱,我怀疑这是因为您没有使用for循环或while循环来代替递归。此外,您应该尽可能避免使用
全局变量,因为它们也会混淆程序的控制流,尤其是递归。建议:停止使用递归建议:停止使用递归是的。问题出在哪里。我已经改变了它,到目前为止,它的工作。谢谢:)是的,你说得对。问题出在哪里。我已经改变了它,到目前为止,它的工作。谢谢:)我已删除try/except并更改了它。目前它正在发挥作用。但如果有错误,我会尝试你的想法。谢谢:)我已删除try/except并更改了它。目前它正在发挥作用。但如果有错误,我会尝试你的想法。谢谢:)