If conditions are not met, want to retry several times, how to operate this logic?

for example, set a judgment condition:

if requests.get(so_url,proxies=proxy,headers=headers).status_code == 200:
    html = requests.get(so_url,proxies=proxy,headers=headers).text
    url = re.search("<div class="img-box">.*?<a.*?href="(.*?)"><span>",html,re.S).group(1).replace("amp;","")
    print(url)
else:
    proxy = str(proxy).replace("{"http": "http://","").replace(""}","")
    -sharp

how should this logic be designed?
calls this function directly under else at first, but then seems to have a memory overflow ?
then simply write the above judgment again under else, but can only try again.

the desired effect is that if you get to else, retry the judgment condition N times.

Aug.27,2021

count = 0
MAX_TRY=9
while (count < MAX_TRY):
   print 'The count is:', count
   if requests.get(so_url,proxies=proxy,headers=headers).status_code == 200:
        html = requests.get(so_url,proxies=proxy,headers=headers).text
        url = re.search('<div class="img-box">.*?<a.*?href="(.*?)"><span>',html,re.S).group(1).replace('amp;','')
        print(url)
        break
   else:
        proxy = str(proxy).replace("{'http': 'http://",'').replace("'}",'')
        -sharp
        count = count + 1
   

Recursive. You need a variable to record the number of requests


use Session () object

python3

import requests as req

ssn = req.Session()
ssn.headers=headers

n=9
for i in range(n):
    ssn.proxies = proxy
    rsp = ssn.get(url)
    if rsp.status_code == 200:
        html=rsp.text
        print(url)
        break
    else:
        proxy = str(proxy).replace("{'http': 'http://",'').replace("'}",'')

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e52d5a-44729.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e52d5a-44729.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?