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("'}",'')

Menu